Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impr(KeyValue): KeyValue is now independent #5614

Open
wants to merge 9 commits into
base: nextgen
Choose a base branch
from

Conversation

sqlerrorthing
Copy link
Contributor

@sqlerrorthing sqlerrorthing commented Feb 14, 2025

I noticed that the code for handling [ValueType.KEY] looks "clumsy"

now, u can use

@Suppress("unused")
private val someKey by key("Key").onPress {
    /* action */
}

instead of

private val someKey by key("Key")

@Suppress("unused")
private val keyboardHandler = handler<KeyEvent> {
    if (it.action != GLFW.GLFW_PRESS || mc.currentScreen != null) {
        return@handler
    }

    when (it.key.code) {
        someKey.code -> {
            /* action */
        }
    }
}

@Suppress("unused")
private val mouseHandler = handler<MouseButtonEvent> {
    if (it.action != GLFW.GLFW_PRESS || mc.currentScreen != null) {
        return@handler
    }

    if (someKey.category == InputUtil.Type.MOUSE && someKey.code == it.button) {
        /* action */
    }
}
  1. If you don't want to handle the key press like this above (because this code essentially does the same thing), then pass parent as null in key().
    private val controlKey by key(null, "Key", InputUtil.GLFW_KEY_LEFT_CONTROL)
    
    val shouldBeGoing
        get() = running && controlKey != InputUtil.UNKNOWN_KEY &&
                    InputUtil.isKeyPressed(mc.window.handle, controlKey.code)
  2. if you want to handle the key press without [mc.currentScreen] check, set canExecuteInMenu to true in key().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants