From 06ef6be0fd0b7085c7121f100dff354311287e2b Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:34:42 +0200 Subject: code cleanup, There are no Utils registered as listeners, and replacing OSUtils.isKeyHeld(key) to key.isKeyHeld() --- .../at/hannibal2/skyhanni/utils/KeyboardManager.kt | 78 ++++++++++++++++++++++ .../at/hannibal2/skyhanni/utils/KeyboardUtils.kt | 53 --------------- .../java/at/hannibal2/skyhanni/utils/OSUtils.kt | 27 -------- 3 files changed, 78 insertions(+), 80 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/utils/KeyboardManager.kt delete mode 100644 src/main/java/at/hannibal2/skyhanni/utils/KeyboardUtils.kt (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/KeyboardManager.kt b/src/main/java/at/hannibal2/skyhanni/utils/KeyboardManager.kt new file mode 100644 index 000000000..21a17e740 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/KeyboardManager.kt @@ -0,0 +1,78 @@ +package at.hannibal2.skyhanni.utils + +import at.hannibal2.skyhanni.events.LorenzKeyPressEvent +import at.hannibal2.skyhanni.events.LorenzTickEvent +import io.github.moulberry.moulconfig.internal.KeybindHelper +import net.minecraft.client.settings.KeyBinding +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import org.apache.commons.lang3.SystemUtils +import org.lwjgl.input.Keyboard +import org.lwjgl.input.Mouse + +object KeyboardManager { + private var lastClickedMouseButton = -1 + + // A mac-only key, represents Windows key on windows (but different key code) + fun isCommandKeyDown() = Keyboard.KEY_LMETA.isKeyHeld() || Keyboard.KEY_RMETA.isKeyHeld() + fun isControlKeyDown() = Keyboard.KEY_LCONTROL.isKeyHeld() || Keyboard.KEY_RCONTROL.isKeyHeld() + fun isShiftKeyDown() = Keyboard.KEY_LSHIFT.isKeyHeld() || Keyboard.KEY_RSHIFT.isKeyHeld() + + fun isPastingKeysDown(): Boolean { + val modifierHeld = if (SystemUtils.IS_OS_MAC) isCommandKeyDown() else isControlKeyDown() + return modifierHeld && Keyboard.KEY_V.isKeyHeld() + } + + @SubscribeEvent + fun onTick(event: LorenzTickEvent) { + if (Mouse.getEventButtonState() && Mouse.getEventButton() != -1) { + val key = Mouse.getEventButton() - 100 + LorenzKeyPressEvent(key).postAndCatch() + lastClickedMouseButton = key + return + } + + if (Keyboard.getEventKeyState() && Keyboard.getEventKey() != 0) { + val key = Keyboard.getEventKey() + LorenzKeyPressEvent(key).postAndCatch() + lastClickedMouseButton = -1 + return + } + + if (Mouse.getEventButton() == -1 && lastClickedMouseButton != -1) { + if (lastClickedMouseButton.isKeyHeld()) { + LorenzKeyPressEvent(lastClickedMouseButton).postAndCatch() + println("still holding") + return + } + lastClickedMouseButton = -1 + } + + // I don't know when this is needed + if (Keyboard.getEventKey() == 0) { + LorenzKeyPressEvent(Keyboard.getEventCharacter().code + 256).postAndCatch() + } + } + + fun KeyBinding.isActive(): Boolean { + if (!Keyboard.isCreated()) return false + try { + if (keyCode.isKeyHeld()) return true + } catch (e: IndexOutOfBoundsException) { + println("KeyBinding isActive caused an IndexOutOfBoundsException with keyCode: $keyCode") + e.printStackTrace() + return false + } + return this.isKeyDown || this.isPressed + } + + fun Int.isKeyHeld(): Boolean { + if (this == 0) return false + return if (this < 0) { + Mouse.isButtonDown(this + 100) + } else { + KeybindHelper.isKeyDown(this) + } + } + + fun getKeyName(keyCode: Int): String = KeybindHelper.getKeyName(keyCode) +} \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/KeyboardUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/KeyboardUtils.kt deleted file mode 100644 index b06a7621e..000000000 --- a/src/main/java/at/hannibal2/skyhanni/utils/KeyboardUtils.kt +++ /dev/null @@ -1,53 +0,0 @@ -package at.hannibal2.skyhanni.utils - -import at.hannibal2.skyhanni.events.LorenzKeyPressEvent -import at.hannibal2.skyhanni.events.LorenzTickEvent -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import org.apache.commons.lang3.SystemUtils -import org.lwjgl.input.Keyboard -import org.lwjgl.input.Mouse - -object KeyboardUtils { - private var lastClickedMouseButton = -1 - - // A mac-only key, represents Windows key on windows (but different key code) - fun isCommandKeyDown() = OSUtils.isKeyHeld(Keyboard.KEY_LMETA) || OSUtils.isKeyHeld(Keyboard.KEY_RMETA) - fun isControlKeyDown() = OSUtils.isKeyHeld(Keyboard.KEY_LCONTROL) || OSUtils.isKeyHeld(Keyboard.KEY_RCONTROL) - fun isShiftKeyDown() = OSUtils.isKeyHeld(Keyboard.KEY_LSHIFT) || OSUtils.isKeyHeld(Keyboard.KEY_RSHIFT) - - fun isPastingKeysDown(): Boolean { - val modifierHeld = if (SystemUtils.IS_OS_MAC) isCommandKeyDown() else isControlKeyDown() - return modifierHeld && OSUtils.isKeyHeld(Keyboard.KEY_V) - } - - @SubscribeEvent - fun onTick(event: LorenzTickEvent) { - if (Mouse.getEventButtonState() && Mouse.getEventButton() != -1) { - val key = Mouse.getEventButton() - 100 - LorenzKeyPressEvent(key).postAndCatch() - lastClickedMouseButton = key - return - } - - if (Keyboard.getEventKeyState() && Keyboard.getEventKey() != 0) { - val key = Keyboard.getEventKey() - LorenzKeyPressEvent(key).postAndCatch() - lastClickedMouseButton = -1 - return - } - - if (Mouse.getEventButton() == -1 && lastClickedMouseButton != -1) { - if (OSUtils.isKeyHeld(lastClickedMouseButton)) { - LorenzKeyPressEvent(lastClickedMouseButton).postAndCatch() - println("still holding") - return - } - lastClickedMouseButton = -1 - } - - // I don't know when this is needed - if (Keyboard.getEventKey() == 0) { - LorenzKeyPressEvent(Keyboard.getEventCharacter().code + 256).postAndCatch() - } - } -} \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt index 981ea87c3..b083b2d3b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt @@ -1,9 +1,5 @@ package at.hannibal2.skyhanni.utils -import io.github.moulberry.moulconfig.internal.KeybindHelper -import net.minecraft.client.settings.KeyBinding -import org.lwjgl.input.Keyboard -import org.lwjgl.input.Mouse import java.awt.Desktop import java.io.IOException import java.net.URI @@ -30,27 +26,4 @@ object OSUtils { } suspend fun readFromClipboard() = ClipboardUtils.readFromClipboard() - - fun KeyBinding.isActive(): Boolean { - if (!Keyboard.isCreated()) return false - try { - if (isKeyHeld(keyCode)) return true - } catch (e: IndexOutOfBoundsException) { - println("KeyBinding isActive caused an IndexOutOfBoundsException with keyCode: $keyCode") - e.printStackTrace() - return false - } - return this.isKeyDown || this.isPressed - } - - fun isKeyHeld(keyCode: Int): Boolean { - if (keyCode == 0) return false - return if (keyCode < 0) { - Mouse.isButtonDown(keyCode + 100) - } else { - KeybindHelper.isKeyDown(keyCode) - } - } - - fun getKeyName(keyCode: Int): String = KeybindHelper.getKeyName(keyCode) } -- cgit