Input and modifier keys

borisasborisas Posts: 2Member

Hello,
Recently i ran into an issue.I needed a key to react to tapping, so i moved from Input.is_key_pressed(...) to the _input function key handling with key map.
The problem i ran into is that using WASD and having the user hold shitf doesn't release the key pressed (for example : Hold A, Press Shift, Release A, Press D: the event that releases A is never called). Did some research found out that this is the cause of modifier keys and binding A to A, Shift+A fixes this issue, but i find this a really poor fix to the problem, honestly it just seems extremely stupid, not to mention that i need to bind CTRL and ALT the same way for it to work. This kind of solution seems unacceptable to me and i didn't manage to find a proper solution to this.
Of course, i might be able to separate keys so that tapping is captured in _input function and everything else to work through Input.is_key_pressed, but that also seems like a half assed solution to the problem.
Is there a proper solution to this or is the only solution modifying source and rebuilding (which is an even worse solution than the ones i mentioned)?

Answers

  • RossRoss Posts: 199Member
    Answer ✓

    See: https://github.com/godotengine/godot/issues/6826

    Basically they designed Godot's input action system around the assumption that you would be using modifier keys as modifiers and never like normal keys. Presumably because that's how editors usually work. But of course that screws over anyone who wants all the keys to just work like keys. It sounds like they may have fixed it, but I haven't tried a recent build yet.

    If you want to use an older or official release version, you can solve this by bypassing the input action map and dealing with key input directly. This little snippet will print out the name of the key for each event and if it was pressed or released (ignoring key repeats, or "echoes").

    func _input(event):
        if event.type == InputEvent.KEY and not event.is_echo():
            var state = "pressed"
            if not event.pressed: state = "released"
            print(OS.get_scancode_string(event.scancode) + " " + state)
    

    You could set up your own global input map script with the scancodes if you want things to be less hard-coded.

    The only trouble comes if you want customizable bindings that may not be keys—mouse buttons, mouse movement, gamepad joystick axes, or gamepad buttons. You'll just have to do some more work to remember and check what kind of input you're getting, rather than just needing the key scancode. Of course you have to do that with Godot's built-in input map too, so I guess it's a moot point.

  • borisasborisas Posts: 2Member

    Thanks! this was what i was looking for.

    Currently i'm using the stable build and just use steam version to keep it auto updated.

    Read that issue thread before writing this post, but it seemed like it's not in the stable version yet, because it definitely did not work the way i needed it to.

Sign In or Register to comment.