aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2023-10-12 22:31:49 +1100
committerGitHub <noreply@github.com>2023-10-12 13:31:49 +0200
commitdb0cd17c7019c072a5043ae8d0dca8ce6459fb32 (patch)
tree3e8a33b6d434d02d637e596eecd707d4e8c17495 /src/main/java/at/hannibal2/skyhanni/utils
parentd4084e697899f6dc3ceb87b4d229e0fe3819440a (diff)
downloadskyhanni-db0cd17c7019c072a5043ae8d0dca8ce6459fb32.tar.gz
skyhanni-db0cd17c7019c072a5043ae8d0dca8ce6459fb32.tar.bz2
skyhanni-db0cd17c7019c072a5043ae8d0dca8ce6459fb32.zip
Fix + Backend: Made mouse buttons work as key binds (#560)
Fix + Backend: Made mouse buttons work as key binds #560
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/KeyboardUtils.kt53
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt14
2 files changed, 53 insertions, 14 deletions
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 <T> onChange(vararg properties: Property<out T>, observer: Observer<T>) {
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> T.transformIf(condition: T.() -> Boolean, transofmration: T.() -> T) =