Keyboard
This page contains information and code snippets for detecting keyboard inputs from the client.
Detecting a key input
There is no one "correct" way of detecting a key input, but the easiest is to mixin into KeyboardHandler
java
@Mixin(KeyboardHandler.class)
public class KeyboardHandlerMixin {
@Inject(method = "keyPress", at = @At("HEAD"), cancellable = true)
private void onKeyPress(long window, int action, KeyEvent event, CallbackInfo ci) {
if (action == 1) {
// Key pressed
} else if (action == 0) {
// Key released
}
}
}Checking if a key is pressed
The two primary ways to check if a key is pressed are:
Using Minecraft's InputConstants
kotlin
fun isPressed(key: Int): Boolean {
return InputConstants.isKeyDown(client.window.handle(), key)
}Using GLFW
kotlin
fun isPressed(key: Int): Boolean {
val state = GLFW.glfwGetKey(client.window.handle(), key)
return state == GLFW.GLFW_PRESS || state == GLFW.GLFW_REPEAT
}