Skip to content

Mouse

This page contains information and code snippets for detecting mouse inputs from the client.

Detecting mouse input

There is no one "correct" way of detecting a mouse input, but the easiest is to mixin into MouseHandler

java
@Mixin(MouseHandler.class)
public class MouseHandlerMixin {
    @Inject(method = "onButton", at = @At("HEAD"), cancellable = true)
    private void onButton(long window, MouseButtonInfo buttonInfo, int action, CallbackInfo ci) {
        if (action == 1) {
            // Mouse button pressed
        } else if (action == 0) {
            // Mouse button released
        }
    }
}

Detecting mouse scroll

java
@Mixin(MouseHandler.class)
public class MouseHandlerMixin {
    @Inject(method = "onScroll", at = @At("HEAD"), cancellable = true)
    private void onScroll(long windowPointer, double scrollX, double scrollY, CallbackInfo ci) {
        // Mouse scrolled!
    }
}

Detecting mouse movement

java
@Mixin(MouseHandler.class)
public class MouseHandlerMixin {
    @Shadow
    private double xpos;

    @Shadow
    private double ypos;

    @Inject(method = "onMove", at = @At("HEAD"), cancellable = true)
    private void onMove(long windowPointer, double xpos, double ypos, CallbackInfo ci) {
        // Mouse moved!

        // If you want to cancel the mouse move, you should do:
        // ci.cancel();
        // this.xpos = xpos;
        // this.ypos = ypos;
    }
}

Getting mouse X and Y positions

kotlin
fun mouseX(): Float {
    return client.mouseHandler.xpos().toFloat() * client.window.guiScaledWidth / max(1, client.window.width)
}

fun mouseY(): Float {
    return client.mouseHandler.ypos().toFloat() * client.window.guiScaledHeight / max(1, client.window.height)
}

Checking if a mouse button is pressed

kotlin
fun isPressed(button: Int): Boolean {
    val state = GLFW.glfwGetMouseButton(client.window.handle(), button)
    return state == GLFW.GLFW_PRESS || state == GLFW.GLFW_REPEAT
}

Not affiliated with Mojang, Microsoft, or Hypixel.