Skip to content

Commit 31c10f8

Browse files
committed
Post key events on press and release
1 parent 9eb75dc commit 31c10f8

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

common/src/main/java/com/lambda/mixin/input/KeyboardMixin.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public class KeyboardMixin {
3030
@Inject(method = "onKey", at = @At("HEAD"))
3131
private void onKey(long window, int key, int scancode, int action, int modifiers, CallbackInfo ci) {
3232
if (key <= 0) return;
33-
if (action != 1) return; // TODO: Post events on both press and release ?
34-
3533
EventFlow.post(new KeyboardEvent.Press(key, scancode, action, modifiers));
3634
}
3735

common/src/main/kotlin/com/lambda/event/events/KeyboardEvent.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ package com.lambda.event.events
1919

2020
import com.lambda.event.Event
2121
import com.lambda.util.KeyCode
22+
import org.lwjgl.glfw.GLFW.GLFW_MOD_ALT
23+
import org.lwjgl.glfw.GLFW.GLFW_MOD_CAPS_LOCK
24+
import org.lwjgl.glfw.GLFW.GLFW_MOD_CONTROL
25+
import org.lwjgl.glfw.GLFW.GLFW_MOD_NUM_LOCK
26+
import org.lwjgl.glfw.GLFW.GLFW_MOD_SHIFT
27+
import org.lwjgl.glfw.GLFW.GLFW_MOD_SUPER
28+
import org.lwjgl.glfw.GLFW.GLFW_PRESS
29+
import org.lwjgl.glfw.GLFW.GLFW_RELEASE
2230

2331
sealed class KeyboardEvent {
2432
/**
@@ -37,12 +45,25 @@ sealed class KeyboardEvent {
3745
val action: Int,
3846
val modifiers: Int,
3947
) : Event {
48+
/**
49+
* Maps the scancode to the US layout
50+
*/
4051
val translated: KeyCode
4152
get() = KeyCode.virtualMapUS(keyCode, scanCode)
53+
54+
val isPressed = action == GLFW_PRESS
55+
val isReleased = action == GLFW_RELEASE
56+
57+
val hasShift = modifiers and GLFW_MOD_SHIFT != 0
58+
val hasControl = modifiers and GLFW_MOD_CONTROL != 0
59+
val hasClt = modifiers and GLFW_MOD_ALT != 0
60+
val hasSuper = modifiers and GLFW_MOD_SUPER != 0
61+
val hasCapsLock = modifiers and GLFW_MOD_CAPS_LOCK != 0
62+
val hasNumLock = modifiers and GLFW_MOD_NUM_LOCK != 0
4263
}
4364

4465
/**
45-
* Represents glfwSetKeyCallback events
66+
* Represents glfwSetCharCallback events
4667
*
4768
* Keys and characters do not map 1:1.
4869
* A single key press may produce several characters, and a single

common/src/main/kotlin/com/lambda/module/Module.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ abstract class Module(
130130
init {
131131
listen<KeyboardEvent.Press>(alwaysListen = true) { event ->
132132
if (mc.options.commandKey.isPressed) return@listen
133+
if (!event.isPressed) return@listen
133134
if (keybind == KeyCode.UNBOUND) return@listen
134135
if (event.translated != keybind) return@listen
135136
if (mc.currentScreen != null) return@listen

common/src/main/kotlin/com/lambda/module/modules/player/Replay.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ object Replay : Module(
113113

114114
init {
115115
listen<KeyboardEvent.Press> {
116+
if (!it.isPressed) return@listen
116117
if (mc.currentScreen != null && !mc.options.commandKey.isPressed) return@listen
117118

118119
when (it.translated) {

0 commit comments

Comments
 (0)