Search Unity

How do you know which keyboard key was pressed?

Discussion in 'Input System' started by stevphie123, Jan 24, 2022.

  1. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    82
    How to know which button was pressed in the new inputSystem? I've been scratching my head now, just to make my typing game works with the new inputsystem.
     
  2. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    82
    This works

    Code (CSharp):
    1.        
    2. InputSystem.onEvent
    3.         .ForDevice<Keyboard>()
    4.         .Where(e =>
    5.         {
    6.             if(e.type != StateEvent.Type && e.type != DeltaStateEvent.Type)
    7.             return false;
    8.             else
    9.             return e.HasButtonPress();
    10.         })
    11.         .Call(ctrl =>
    12.         {  
    13.             foreach (var button in ctrl.GetAllButtonPresses())
    14.             Debug.Log($"{button} was pressed");
    15.         });
    But who thought it was much better to do this compared to the legacy? I'm baffled by how hard just to do small things now
     
  3. andrew_oc

    andrew_oc

    Unity Technologies

    Joined:
    Apr 16, 2021
    Posts:
    77
  4. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    82
    Thats the method I used before, but that requires separate bindings for other keys by the end-users and that's not ideal bcos I'm making a typing game framework, so it must fit all cases.

    Further to that onTextInput keeps spewing "InvalidOperationException: Already have an event buffer set! Was OnUpdate() called recursively?" when I bind some other keys (enter, space, etc...) even tho I did not do anything recursively, this was a bug from prior v1.1 preview which should be fixed in v.1.1.1 which also the version I'm using for my typing game framework.

    So the use case here is freedom for both dev who provides the easy-to-use-feature and end-users who will use the framework without having to tinker around, generating file this/that, setting actions here/there.. like oh my.. (big G here)...

    Think about this, there are people who depends on full 3rd party framework in Unity engine as a whole without having to know every technical aspects of Unity itself, which also what I'm trying to provide to those who will use my typing game framework, and it's been one hellish of an experience let me tell you.

    Either way, props for the Unity InputSystem team who made it happen. I will still use it (because no other better choice!)
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    For reacting to any key press,
    onAnyButtonPress
    is probably the easiest way.

    Code (CSharp):
    1. InputSystem.onAnyButtonPress.Call(
    2.     button =>
    3.     {
    4.         if (button is KeyControl key)
    5.             Debug.Log($"Key {key.name} pressed! (text: {key.displayName})");
    6.     });
    (treating
    displayName
    as typed text generally isn't correct but for a typing game not an altogether unreasonable assumption to make)

    Another way (not terribly efficient):

    Code (CSharp):
    1. var action = new InputAction(binding: "<Keyboard>/*");
    2. action.Enable();
    3.  
    4. action.performed +=
    5.     ctx => {
    6.         if (ctx.control is KeyControl key && !key.synthetic) // Synthetic is stuff like anyKey.
    7.             Debug.Log($"Key {key.name} pressed! (text: {key.displayName})");
    8.     };
     
    AnaLuyza likes this.
  6. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    82
    Note to anybody stumbles upon this post.

    The solution @Rene-Damm posted for onAnyButtonPress would throw exception bcos of this bug

    it was fixed in v1.3


    Also, is it possible to get the deviceId with onAnyButtonPress?
     
    Last edited: Feb 7, 2022
  7. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Code (CSharp):
    1. InputSystem.onAnyButtonPress.Call(
    2.     ctrl =>
    3.     {
    4.         Debug.Log($"Button {ctrl} on device {ctrl.device} with ID {ctrl.device.deviceId} was pressed");
    5.     });
     
    stevphie123 likes this.
  8. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    82
    This just recently added? Pretty sure, I can't do this in v1.1

    Edit: I did it wrong, i thought I could just ctr.deviceId... so yeah, this works