How to transform a US layout key to a location based key?

There’s a feature in the new input system that automatically transforms a US layout key to the one the player is using.

I would like to have access to this functionality in script so that I can show the player which key he has to press no matter his location (his keyboard layout). Any ideas how I can do that?

yourInputActionReference.action.controls[0].displayName will show what you want !

Is there a method in the new input system that transforms a string to a key, gamepadButton or anything like that? Because doing this for all the keys would be tedious

    public Key StringToKey(string _displayName)
    {
        if (Compare("A", _displayName)) return Key.A;
        if (Compare("B", _displayName)) return Key.B;
        if (Compare("C", _displayName)) return Key.C;
    }

If you want to map it and respect keyboard layout, you’ll have to first go from display name and then back to Key via KeyControl’s keyCode property.

public Key DisplayNameToKey(string displayName)
{
    // Note: nice wrapper method to look up keys by display name is on the list.
    Keyboard.current.allKeys.FirstOrDefault(x => x.displayName = displayName)?.keyCode;

If you want to map based on US keyboard layout, you can do

((KeyControl)Keyboard.current[displayName]).keyCode
1 Like

That’s great! Is there a way to do the same for the GamepadButton enum?

 var button = ((ButtonControl)Gamepad.current[displayName]).keycode????

Hi there, sorry for reviving an old topic.
We have means to remap keyboard-only entries in our game and I’d like to display the actual keyboard’s key in the UI.
Which is what KeyControl.displayName does.
However, action.controls[0] assumes that the current scheme uses the keyboard, which may not be the case when I try to display the proper string.

So instead I try to retrieve the first binding that has the “keyboard&mouse” scheme in its groups and then retrieve the KeyControl based on this binding:

var control = InputSystem.FindControl(binding.effectivePath);
if (control is KeyControl keyControl)
    str = keyControl.displayName;
else
    str = binding.ToDisplayString();

Is there any nicer/less hacky way to do this, without having to go through the controls?