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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How to find Key that was pressed in new Input System

Discussion in 'Input System' started by VideoVillain, Jan 30, 2021.

  1. VideoVillain

    VideoVillain

    Joined:
    Oct 12, 2020
    Posts:
    6
    All I want to be able to do is find what key was pressed in my code, but I'm really struggling and I feel like I must have missed something because it shouldn't be this hard.

    I have an action map for keyboard input, with an action for a key press with Any Key [Keyboard] as the binding and "Press" as the interaction.

    The delegate is so other classes can subscribe to the OnPress event.

    Code (CSharp):
    1. public delegate void KeyPressEvent(???);
    2. public event KeyPressEvent OnPress;
    3.  
    4. myControls.Keyboard.KeyPress.started += _context => Press(_context);
    5.  
    6. private void Press(InputAction.CallbackContext _context) {
    7.             Log("[" + _context + "] key pressed.");
    8.             OnPress(myControls.Keyboard.KeyPress.???);
    9. }
    10.  
    I have no idea what to use for ??? though. I've tried float, but it's just 0~1 regardless of the key I press.
    When I call OnPress(myControls.Keyboard.KeyPress.???) I have no idea what works to get the key that was pressed. I tried converting it to Key but that gives an error, I also can't covert to KeyControl either.

    I've tried things like:
    • (Key)myControls.Keyboard.KeyPress.ReadValue<float>()
    • myControls.Keyboard.KeyPress.ReadValue<Key>()
    • (KeyControl)myControls.Keyboard.KeyPress.ReadValue<float>()
    • myControls.Keyboard.KeyPress.ReadValue<KeyControl>()
    • (Key)myControls.Keyboard.KeyPress.ReadValueAsObject()
    • myControls.Keyboard.KeyPress
    and many others. It just won't let me get the key that was pressed.

    Please help!
     
  2. VideoVillain

    VideoVillain

    Joined:
    Oct 12, 2020
    Posts:
    6
    I would really like any input at all.

    I know I can make a separate action for each specific key, and then map it's subscribed to function to do what I want, but that would be ridiculous to do for each key...

    I also know I can use Keyboard.current.someKey.wasPressedThisFrame, but then it has to be in Update() and doesn't belong to the action map at all AND has to be in the Update() function since it can't be subscribed to (afaik).

    Is there really no way to just get what key was pressed from the ActionMap-Actions-Any Key [Keyboard] binding?
     
  3. VideoVillain

    VideoVillain

    Joined:
    Oct 12, 2020
    Posts:
    6
    Hello @Rene-Damm , sorry to bother you but could you help me get this answered maybe? It doesn't seem anyone else knows.