blob: 3314bde7fd57463b14016332b9f58eee8fb8cac5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package at.hannibal2.skyhanni.utils
import at.hannibal2.skyhanni.events.LorenzKeyPressEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import io.github.moulberry.moulconfig.gui.GuiScreenElementWrapper
import io.github.moulberry.moulconfig.internal.KeybindHelper
import net.minecraft.client.Minecraft
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() = isModifierHeld() && Keyboard.KEY_V.isKeyHeld()
fun isCopyingKeysDown() = isModifierHeld() && Keyboard.KEY_C.isKeyHeld()
private fun isModifierHeld() = if (SystemUtils.IS_OS_MAC) isCommandKeyDown() else isControlKeyDown()
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
val isConfigScreen = Minecraft.getMinecraft().currentScreen is GuiScreenElementWrapper
if (isConfigScreen) return
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()
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)
}
|