Handling input of key combinations

Godot Version

4.4, Linux/X11

Question

How to handle input of keys/characters that are triggered by a combination of keys?

Use case: I’d like to assign a special function to “keys” (actually characters) [<] and [>]. Preferably using Input assignment in Project Settings and actions (same as I do for all the other input).

Problem: In many keyboard layouts, these characters aren’t triggered by single keystrokes, but by combinations. E.g. in English layout, the two are typed by pressing [Shift]+[,] and [Shift]+[.] respectively. In German layout, it’s even worse, since both characters are placed on the same key, so pressing [<] alone produces “<”, and [Shift]+[<] produces “>”.

How I handle inputs in my game is that I add actions to project settings, assign them keys and in my code, I check for them by calling event.is_action_pressed('blahblah') in _input or _unhandled_input functions.

I tried to make it work the same way for [<] and [>] by creating actions and assigning them with “Less” and “Greater”. But, whatever I try, I always end up getting InputEventKeys with keycode of basic keys (e.g. “Period”) and Shift modifier. Switching between physical/non-physical doesn’t help. Unicode seems to not work at all :frowning: I tried _input, _unhandled_input, _unhandled_key_input, and _shortcut_input, but none of these seems to be aware that I’m pressing a combination.

What’s the Godot way of handling this kind of inputs?

(note: I’m aware that it’s kind of mixing concepts of “keys” and “characters”, and they’re not the same, but tbh players won’t care. If they’re told to “Press key [<]”, they’ll understand that they’re expected to press either a single key or a combination, depending on the layout they use)

I think it’s easier to decide on a physical key you want an action to be bound to, rather than a character. Listening to input by monitoring what character the keyboard would actually end up typing sounds messy and in most cases a bad idea.

I would decide on a physical key, or in your case, two, then during input checking, check If Input.IsActionPressed("action") && Input.IsActionPressed("otherAction") then do something, rather than monitor / listen to what character is being typed, as this would be much, much more difficult when you take other keyboard layouts into account, as you mentioned. There are functions you can use to translate a physical key into what character that’s supposed to be on the current user’s keyboard.