Search Unity

InputSystem.onEvent inconsistencies with mouse buttons and wasReleasedThisFrame

Discussion in 'Input System' started by Crayz, Jan 26, 2020.

  1. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    edit: moving the code to InputSystem.onAfterUpdate solved my issue.

    I am using version 1.0.0 released Jan 24, 2020

    Code (CSharp):
    1.         private void InputSystem_onEvent(InputEventPtr eventPtr, InputDevice device)
    2.         {
    3.             //if (!eventPtr.IsA<StateEvent>() && !eventPtr.IsA<DeltaStateEvent>())
    4.             //{
    5.             //    return;
    6.             //}
    7.  
    8.             if (device is Mouse mouse)
    9.             {
    10.                 foreach(var ctrl in mouse.allControls)
    11.                 {
    12.                     if(ctrl is ButtonControl btn)
    13.                     {
    14.                         if (btn.wasPressedThisFrame)
    15.                         {
    16.                             Debug.Log("PRESS: " + ctrl.name);
    17.                         }
    18.                         if (btn.wasReleasedThisFrame)
    19.                         {
    20.                             Debug.Log("RELEASE: " + ctrl.name);
    21.                         }
    22.                     }
    23.                 }
    24.             }
    25.  
    26.             if (device is Keyboard keyboard)
    27.             {
    28.                 foreach(var ctrl in keyboard.allControls)
    29.                 {
    30.                     if(ctrl is ButtonControl btn)
    31.                     {
    32.                         if (btn.name == "anyKey")
    33.                         {
    34.                             continue;
    35.                         }
    36.                         if (btn.wasPressedThisFrame)
    37.                         {
    38.                             Debug.Log("PRESS: " + ctrl.name);
    39.                         }
    40.                         if (btn.wasReleasedThisFrame)
    41.                         {
    42.                             Debug.Log("RELEASE: " + ctrl.name);
    43.                         }
    44.                     }
    45.                 }
    46.             }
    47.         }
    I would like to use the new InputSystem strictly via script to move away from polling input on update, the above code is not working as expected. For mouse controls, wasPressedThisFrame and wasReleasedThisFrame only trigger if the mouse is also moving side to side or up and down, not if the mouse is perfectly still. Side buttons (back/forward) also appear to be less consistent.

    Keyboard ButtonControls wasPressedThisFrame works as expected, but wasReleasedThisFrame is never true.

    Am I using the API wrong?
     
    Last edited: Jan 26, 2020
  2. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    Same issue.

    this works perfectly fine:
    Code (CSharp):
    1. if (UnityEngine.InputSystem.Mouse.current.leftButton.wasReleasedThisFrame)
    2. {
    3.     Debug.Log("leftButton.wasReleasedThisFrame");
    4. }


    while this is not executed when the mouse button is released:
    Code (CSharp):
    1. if (_controls.Player.Fire.activeControl != null)
    2. {
    3.     fireButton = _controls.Player.Fire.activeControl as ButtonControl;
    4.     if (fireButton.wasReleasedThisFrame) Debug.Log("FB Relased this Frame");
    5. }


    It works when the (x-box) controller button is released but not on mouseUp.
    Down works perfectly fine, Up doesn't.

    Unplugging the controller changed nothing.

    Edit: isPressed also works fine
     
  3. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    Now I think I understand the problem - by changing my code to:

    Code (CSharp):
    1. if (_controls.Player.AltFire.activeControl != null)
    2. {
    3.     altFireButton = _controls.Player.AltFire.activeControl as ButtonControl;
    4. }
    5. if (altFireButton.wasReleasedThisFrame) Debug.Log("AFB Relased this Frame");
    I got it working as expected, seems like the control gets set to null if the button is fully released in one frame - so the controller still had some value on the button when it was released (0.001f or something) so the "activeControl" was still there when the button was released - while on the mouse, it was set immediately to 0f and there was no "activeControl".
     
  4. PhantasmicDev

    PhantasmicDev

    Joined:
    Jul 10, 2013
    Posts:
    35
    Were you able to figure out what's causing wasPressedThisFrame and wasReleasedThisFrame to be true only when moving the mouse? Ran into the same problem.
     
  5. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    I hooked into the InputSystem.onAfterUpdate event instead and it works as expected