Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Check if any key is pressed

Discussion in 'Input System' started by Synpse, Oct 19, 2019.

  1. Synpse

    Synpse

    Joined:
    Mar 19, 2018
    Posts:
    20
    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
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,530
    You can use:
    Code (CSharp):
    1. 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.
     
    Synpse likes this.
  3. Synpse

    Synpse

    Joined:
    Mar 19, 2018
    Posts:
    20
    That might work. Do you think there is a way of getting input from any device? Per example:

    Code (CSharp):
    1. AnyDevice.current.anyKey.wasPressedThisFrame
    Do you think this might be possible?
     
  4. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    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

    Code (CSharp):
    1. var action = new InputAction(type: InputActionType.PassThrough, binding: "*/<Button>");
    2.  
    3. // In update code:
    4. 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.

    Code (CSharp):
    1. // Sets m_ButtonPressed as soon as a button is pressed on
    2. // any device.
    3. InputSystem.onEvent +=
    4.     (eventPtr, device) =>
    5.     {
    6.         if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>())
    7.             return;
    8.         var controls = device.allControls;
    9.         var buttonPressPoint = InputSytem.settings.defaultButtonPressPoint;
    10.         for (var i = 0; i < controls.Count; ++i)
    11.         {
    12.             var control = controls[i] as ButtonControl;
    13.             if (control == null || control.synthetic || control.noisy)
    14.                 continue;
    15.             if (control.ReadValueFromEvent(eventPtr, out var value) && value >= buttonPressPoint)
    16.             {
    17.                 m_ButtonPressed = true;
    18.                 break;
    19.             }
    20.         }
    21.     };
     
    idbrii likes this.
  5. Synpse

    Synpse

    Joined:
    Mar 19, 2018
    Posts:
    20
    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?
     
  6. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    For the most part, yes. We have holes (mostly with lower-level stuff) but both InputControl.synthetic and InputControl.noisy, for example, are documented.
     
  7. Synpse

    Synpse

    Joined:
    Mar 19, 2018
    Posts:
    20
    Thanks for the links!
     
  8. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    259
    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
     
    PersianKiller likes this.
  9. NongBenz

    NongBenz

    Joined:
    Sep 30, 2014
    Posts:
    26
    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?
     
  10. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    259
    @NongBenz I wasn't able to find a non-S***ty 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.
     
  11. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    Is there a simpler way to do this now?
     
  12. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    35
    *bump
    any updates? @ unity
     
    altair2020 and tonycoculuzzi like this.
  13. tonycoculuzzi

    tonycoculuzzi

    Joined:
    Jun 2, 2011
    Posts:
    300
    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.
     
    altair2020, MousePods and FVS like this.
  14. jaypordy

    jaypordy

    Joined:
    Feb 25, 2020
    Posts:
    2
    Any luck with this?
     
    FVS and pawerfulgames like this.
  15. FVS

    FVS

    Joined:
    Aug 10, 2015
    Posts:
    49
    "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?
    upload_2021-7-10_14-44-23.png
     
  16. PapaGrizzle

    PapaGrizzle

    Joined:
    Mar 17, 2018
    Posts:
    2
    Has anyone had any luck finding anything out, as far as a simpler solution?
     
  17. FVS

    FVS

    Joined:
    Aug 10, 2015
    Posts:
    49
    I used #4 and it worked for me
     
  18. CraftedGaming

    CraftedGaming

    Joined:
    Jun 9, 2017
    Posts:
    35
    PapaGrizzle likes this.
  19. PapaGrizzle

    PapaGrizzle

    Joined:
    Mar 17, 2018
    Posts:
    2
    SpicyPaper likes this.
  20. CassioTDS

    CassioTDS

    Joined:
    Jan 21, 2020
    Posts:
    12
    Well, here's the code then (extracted from here):
    Code (CSharp):
    1. public KeyCode getCurrentKeyDown()
    2.     {
    3.         KeyCode finalKeyCode=KeyCode.None;
    4.         foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode))){if (Input.GetKey(kcode)){ finalKeyCode = kcode; }}
    5.         if (finalKeyCode == KeyCode.None)
    6.         {
    7.             //Couldn't find key
    8.         }
    9.         return finalKeyCode;
    10.     }
    upload_2021-8-15_15-21-17.png
     
  21. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Upcoming 1.1-pre.6 finally adds an API for this.

    Code (CSharp):
    1. InputSystem.onAnyButtonPress
    2.     .CallOnce(ctrl => Debug.Log($"{ctrl} pressed"));
     
  22. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    259
    Bless up, thank you very much Rene.
     
  23. watson1423

    watson1423

    Joined:
    Oct 21, 2019
    Posts:
    2
    i se some of you guys have the same problem as me i had solved it for keyboard using the key name like this
    Code (CSharp):
    1. if (Keyboard.current.FindKeyOnCurrentKeyboardLayout(Keyboard.current.allControls[KeyIndex].displayName).wasPressedThisFrame)
    it returns true if the key index is pressed but it dowsnt work for mouse and i havent tried gamepad yet

    edit: Gamepad also doesnt support i am gona do a classic gamer move and just save last state of the buttons with the press tag so it catches diferences
     
    Last edited: Feb 25, 2022
  24. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,805
    https://forum.unity.com/threads/input-system-1-3-0-released-more-fixes.1222632/#post-7862056

    Make sure you read the following discussion too. Don't treat everything as "button", users aren't so no need.
     
  25. Zephni

    Zephni

    Joined:
    Apr 23, 2015
    Posts:
    100
    If anyone else couldn't use this code because .CallOnce wasn't available, make sure to use:

    Code (CSharp):
    1. using UnityEngine.InputSystem.Utilities;
     
    Last edited: Sep 16, 2022