Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Keyboard.current[Key].wasPressedThisFrame fires multiple times before key is released

Discussion in 'Input System' started by Ghosthowl, Jun 30, 2022.

  1. Ghosthowl

    Ghosthowl

    Joined:
    Feb 2, 2014
    Posts:
    228
    How do we get the same behavior when using the new input system and this style of coding? I use this to debug and test stuff quite often but it always gives me mixed results. What I am trying to achieve is the new input system's equivalent of Input.GetKeyDown(). In Update() it almost always triggers 5-20 times in a row, but in FixedUpdate it seems to do it 1-5 times. After some camera input issues, I have changed the 'Update Mode' in Project Settings -> Input System Package to 'Process Events In Dynamic Update' to solve that. This effect seems to have not changed the result.

    I am simply doing:
    Code (CSharp):
    1. Keyboard.current[Key].wasPressedThisFrame
    If this is the intended behavior, then what is the difference between wasPressedThisFrame and isPressed?

    I have temporarily solved it by using this scheme, but it adds a lot of code and I must made a new bool for each key in the script I want to process.

    Code (CSharp):
    1.     private bool keyDown;
    2.  
    3.     private void Update()
    4.     {
    5.         if (Keyboard.current[Key.A].wasPressedThisFrame && Application.isFocused && !keyDown)
    6.         {
    7.             Debug.Log("Pressed");
    8.             keyDown = true;
    9.         }
    10.         if (Keyboard.current[Key.A].wasReleasedThisFrame && Application.isFocused && keyDown)
    11.         {
    12.             keyDown = false;
    13.         }
    14.     }
     
  2. Ghosthowl

    Ghosthowl

    Joined:
    Feb 2, 2014
    Posts:
    228
    Up. Anyone have any ideas here?
     
  3. KingRecycle

    KingRecycle

    Joined:
    Jul 20, 2013
    Posts:
    26
    It is only triggered once per tap for me.

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if( Keyboard.current[Key.A].wasPressedThisFrame ) {
    4.             Debug.Log("Pressed.");      
    5.         }
    6.     }
    Maybe try updating Input package? My setting is Process events in Dynamic update.
     
  4. andrew_oc

    andrew_oc

    Unity Technologies

    Joined:
    Apr 16, 2021
    Posts:
    77
    wasPressedThisFrame is supposed to be the equivalent of GetKeyDown in that it returns true in the frame that a button is pressed. However, there's a quirk in the current implementation where it will return false if a button is both pressed and released in the current frame. If you're using this for automated testing, it would be entirely possible to hit that issue, but it's unclear to me if that's what's happening here. Maybe you can elaborate. What do you mean by "In Update() it almost always triggers 5-20 times in a row"? Do you mean it has a value of true over multiple frames?