Check if any key is pressed

As the title suggests, how can I check if any key was pressed on this new system? Also how can I know what key it was?

Thank you

1 Like

You can use:

Keyboard.current.anyKey.wasPressedThisFrame

It detects if any key was pressed, but I think it doesn't expose the specific key name? I'm just learning this new Input System myself.

2 Likes

That might work. Do you think there is a way of getting input from any device? Per example:

AnyDevice.current.anyKey.wasPressedThisFrame

Do you think this might be possible?

ATM there's no good, simple way to to that. It's on the list for after 1.0 as it's indeed a hole in the system.

Binding-wise, it is possible to do

var action = new InputAction(type: InputActionType.PassThrough, binding: "*/<Button>");

// In update code:
if (action.triggered) //...

but doing so is very inefficient.

An alternative is doing it at the event level but it requires a good chunk of code.

// Sets m_ButtonPressed as soon as a button is pressed on
// any device.
InputSystem.onEvent +=
    (eventPtr, device) =>
    {
        if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>())
            return;
        var controls = device.allControls;
        var buttonPressPoint = InputSytem.settings.defaultButtonPressPoint;
        for (var i = 0; i < controls.Count; ++i)
        {
            var control = controls[i] as ButtonControl;
            if (control == null || control.synthetic || control.noisy)
                continue;
            if (control.ReadValueFromEvent(eventPtr, out var value) && value >= buttonPressPoint)
            {
                m_ButtonPressed = true;
                break;
            }
        }
    };
1 Like

I don't dislike that second alternative. It's ugly but it looks like it will work.
The thing is I don't understand what most of those things do, stuff like "IsA(), controls.synthetic, controls.noisy...". Are those documented on the new input's API?

For the most part, yes. We have holes (mostly with lower-level stuff) but both InputControl.synthetic and InputControl.noisy, for example, are documented.

Thanks for the links!

I'm having an issue with Keyboard.current.anyKey.wasPressedThisFrame. If I hold down one key, then press a second key, it remains false. This is a regression from the old system's Input.anyKey.

Is this intended behavior? If so, is there a workaround? Currently I'm iterating through every Key to individually check if it's down this frame, I feel like there must be a better way...

CC @Rene-Damm -- hope it's okay to ping you

1 Like

I'm experiencing the same issue, "anyKey.wasPressedThisFrame" doesn't trigger when holding a second key so shift/ctrl shortcuts I've set up don't function. We're you able to find a solution/workaround?

@NongBenz I wasn't able to find a non-shitty way of doing it, the workaround I'm using is what I already described: I iterate through every Key and check individually whether one is down.

Is there a simpler way to do this now?

1 Like

*bump
any updates? @ unity

3 Likes

Is this still tedious, or has an easier way been implemented? I have to admit, I can't believe the new input system shipped without an easy way to accomplish this.

4 Likes

Any luck with this?

3 Likes

"Any key" is supposed to be easy, but it seems it's confusing for both the programmers and the customers
Any update on this though?
7314646--887020--upload_2021-7-10_14-44-23.png

10 Likes

Has anyone had any luck finding anything out, as far as a simpler solution?

1 Like


I used #4 and it worked for me

apparently, there's one for the keyboard. haven't tested it yet though.7378889--900194--upload_2021-8-1_17-19-45.png
reference: https://docs.unity3d.com/Packages/com.unity.inputsystem@0.9/manual/Migration.html

Keyboard.current.anyKey.wasUpdatedThisFrame

would anyone like to try the above one out?

1 Like

Dont know how I missed the documentation but this did work.

I ended up using the following and worked great.

Keyboard.current.anyKey.isPressed
1 Like

Well, here's the code then (extracted from here ):

public KeyCode getCurrentKeyDown()
    {
        KeyCode finalKeyCode=KeyCode.None;
        foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode))){if (Input.GetKey(kcode)){ finalKeyCode = kcode; }}
        if (finalKeyCode == KeyCode.None)
        {
            //Couldn't find key
        }
        return finalKeyCode;
    }

7419473--907817--upload_2021-8-15_15-21-17.png

1 Like