From db0cd17c7019c072a5043ae8d0dca8ce6459fb32 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:31:49 +1100 Subject: Fix + Backend: Made mouse buttons work as key binds (#560) Fix + Backend: Made mouse buttons work as key binds #560 --- .../at/hannibal2/skyhanni/utils/KeyboardUtils.kt | 53 ++++++++++++++++++++++ .../at/hannibal2/skyhanni/utils/LorenzUtils.kt | 14 ------ 2 files changed, 53 insertions(+), 14 deletions(-) create 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/KeyboardUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/KeyboardUtils.kt new file mode 100644 index 000000000..b06a7621e --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/KeyboardUtils.kt @@ -0,0 +1,53 @@ +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/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index 392833f65..b5f07ba0d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -24,8 +24,6 @@ import net.minecraft.entity.SharedMonsterAttributes import net.minecraft.event.ClickEvent import net.minecraft.event.HoverEvent import net.minecraft.util.ChatComponentText -import org.apache.commons.lang3.SystemUtils -import org.lwjgl.input.Keyboard import java.awt.Color import java.lang.reflect.Field import java.lang.reflect.Modifier @@ -290,13 +288,6 @@ object LorenzUtils { } } - fun isShiftKeyDown() = OSUtils.isKeyHeld(Keyboard.KEY_LSHIFT) || OSUtils.isKeyHeld(Keyboard.KEY_RSHIFT) - - fun isControlKeyDown() = OSUtils.isKeyHeld(Keyboard.KEY_LCONTROL) || OSUtils.isKeyHeld(Keyboard.KEY_RCONTROL) - - // 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) - // MoulConfig is in Java, I don't want to downgrade this logic fun onChange(vararg properties: Property, observer: Observer) { for (property in properties) { @@ -526,11 +517,6 @@ object LorenzUtils { }, duration.inWholeMilliseconds) } - fun isPastingKeysDown(): Boolean { - val modifierHeld = if (SystemUtils.IS_OS_MAC) isCommandKeyDown() else isControlKeyDown() - return modifierHeld && OSUtils.isKeyHeld(Keyboard.KEY_V) - } - val JsonPrimitive.asIntOrNull get() = takeIf { it.isNumber }?.asInt fun T.transformIf(condition: T.() -> Boolean, transofmration: T.() -> T) = -- cgit