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

Inputsystem.Controls: Key-Input only fires ONCE unless I click into the scene

Discussion in 'Scripting' started by HeinzT0mat0, May 13, 2020.

  1. HeinzT0mat0

    HeinzT0mat0

    Joined:
    Jan 3, 2020
    Posts:
    7
    I just switched to the new Input System 1.0.0 instead of the old "Input Manager" and now seem to have a weird behavior.

    I catch keypress quite simple by the following code:

    Code (CSharp):
    1. protected void Update() {
    2. if (Keyboard.current.leftArrowKey.wasPressedThisFrame)
    3.             {
    4.                Debug.Log( "key down");
    5.             }
    6. if (Keyboard.current.leftArrowKey.wasReleasedThisFrame)
    7.             {
    8.                 Debug.Log("key up");
    9.             }
    10. }
    This script is attached to a not too complex GameObject.

    Now when I press the left key (and keep it pressed) the Debug output is fired only once:
    Code (csharp):
    1. [15:01:48] key down
    2. [15:04:48] key up
    3.  
    When I now in preview mode click anywhere into the scene with my mouse suddenly the event is continuously fired instead each Update:

    Code (csharp):
    1. [15:41:21] key down
    2. [15:41:21] key down
    3. [15:41:21] key down
    4. [15:41:22] key down
    5. ...
    6. [15:41:38] key up
    7. [15:41:38] key up
    8. [15:41:38] key up
    9. ...
    10.  
    In summary: Before clicking anywhere I have exactly ONE output for up and down, after clicking I have a down output for every frame and according ups when releasing the key.

    So my question is:
    How do I get a consistent behavior? Of course I can simply "remember" that keypress in a property and use that for every following update. But is that the way it should be done? And even if I do so: Why does clicking into the scene change the behavior?
     
  2. HeinzT0mat0

    HeinzT0mat0

    Joined:
    Jan 3, 2020
    Posts:
    7
    Ok. I received the wanted result by using "isPressed" instead of "WasPressedInThisFrame". Which makes a lot of sense thinking about it :)

    Still: Why does this behavior change when clicking into the scene?