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

New Input System - Mouse Double Click

Discussion in 'Input System' started by Hallur90, Nov 5, 2020.

  1. Hallur90

    Hallur90

    Joined:
    Dec 4, 2017
    Posts:
    55
    I've been using the new input system for my latest project and was wondering if there was a simple way to detect double clicks with the mouse.

    I thought it was basically just adding a "multi tap" "interaction" to the "Action" but that does not seem to trigger the action.

    I can obviously work around this by hard coding some timer or something but I thought there must be an easier more intuitive way in the new input system, especially since there's a thing called multi tap.

    Untitled.jpg

    Code (CSharp):
    1. public class MouseSelect : MonoBehaviour
    2. {
    3.     private InputActions inputActions;
    4.  
    5.  
    6.     void Awake()
    7.     {
    8.         inputActions = new InputActions();
    9.     }
    10.  
    11.     void OnEnable()
    12.     {
    13.         inputActions.Enable();
    14.     }
    15.  
    16.     void OnDisable()
    17.     {
    18.         inputActions.Disable();
    19.     }
    20.  
    21.     void Start()
    22.     {
    23.         inputActions.Controls.Fire.performed += _ => Select();
    24.         inputActions.Controls.DoubleClick.performed += _ => FocusSelect();
    25.         inputActions.Controls.AltFire.performed += _ =>  Deselect();
    26.     }
    27.  
    28.     private void Select()
    29.     {
    30.         Debug.Log("Select");
    31.     }
    32.  
    33.     private void FocusSelect()
    34.     {
    35.         Debug.Log("Focus select");
    36.     }
    37.  
    38.     private void Deselect()
    39.     {
    40.         Debug.Log("Deselect");
    41.     }
    42. }
    43.  
     
    Last edited: Nov 6, 2020
  2. Hallur90

    Hallur90

    Joined:
    Dec 4, 2017
    Posts:
    55
  3. kasuyakema

    kasuyakema

    Joined:
    Sep 13, 2020
    Posts:
    11
    Im currently having the same Problem. I thought you would add 2 Interactions:
    1.) Multitap
    2.) Tap or Press

    But the Multitap just eats the Input and Tap or Press are never performend.
    The other way around Multitap never gets performed, wich is the correct Behaviour as far as i understand it.

    Edit:
    It works somewhat in this configuration:
    1.)Press (press-only)
    2.)Multitap

    But this will give you an unwanted Press Interaction with every multitap
    If you also want a hold interaction all hope is lost as it seems. I cant find a way to make it work.
     
    Last edited: Nov 10, 2020
  4. Hallur90

    Hallur90

    Joined:
    Dec 4, 2017
    Posts:
    55
    If there is not a way to do this in the new input system I would appreciate if someone could flat out tell me/us, "no you have to create a script to time the pause in-between clicks to determine whether it's a double click".
    Thanks!
     
  5. kasuyakema

    kasuyakema

    Joined:
    Sep 13, 2020
    Posts:
    11
    @Hallur90 Maybe if you mark the Thread as "Help wanted" or "bug" ?
     
    Hallur90 likes this.
  6. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    MultiTap on leftButton would be expected to work (i.e. the setup as shown in the first screenshot). If it doesn't as of 1.1-preview.2, would appreciate a ticket with the Unity bug reporter ("Help >> Report a Bug..." in the editor's main menu) to ensure this gets looked at. IIRC there's a test that sets up exactly this behavior, though, so wondering if something else is up here.
     
  7. kasuyakema

    kasuyakema

    Joined:
    Sep 13, 2020
    Posts:
    11
    @Rene-Damm The Problem is, that the Setup in the Screenshot still requires you to discern if a Mouseclick is part of a double click. With this setup the single click action is performed 2 times during every double click.
    The wanted setup would look like this: (But the multitap Interaction just swallows the Input, Single Tap never occurs)
     

    Attached Files:

  8. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
    Same here, in 1.1.0-perview 2. In multitap, the first click triggers the event, although it is true that the second click doesn't, it gets ignored. The event is also triggered with only one click

    I am not sure if this is expected, because in my opinion, the expected behavior should be that the first click is ignored and the event is triggered at the second click

    I wrote a test about this. @Rene-Damm if you think this not the desired behaviour, I will report the bug

    Code (CSharp):
    1.  
    2.         [UnityTest]
    3.         public IEnumerator DoubleClickTest()
    4.         {
    5.             InputAction doubleClickAction = new InputAction("Double Click");
    6.             doubleClickAction.AddBinding("<Mouse>/leftButton", "Multitap");
    7.             doubleClickAction.Enable();
    8.             int doubleClickCounter = 0;
    9.             doubleClickAction.started += (ctx) => doubleClickCounter++;
    10.  
    11.  
    12.             var mouse = InputSystem.AddDevice<Mouse>();
    13.             InputTestFixture inputTestFixture = new InputTestFixture();
    14.  
    15.             inputTestFixture.PressAndRelease(mouse.leftButton);
    16.             yield return new WaitForSeconds(0.1f);
    17.  
    18.             // Fail. With only 1 click, the event shouldn't be triggered and the counter should be = 0
    19.             Assert.AreEqual(0, doubleClickCounter);
    20.  
    21.             inputTestFixture.PressAndRelease(mouse.leftButton);
    22.             yield return new WaitForSeconds(0.1f);
    23.  
    24.           // Pass. It has been 2 clicks and only 1 event, so this is ok
    25.             Assert.AreEqual(doubleClickCounter, 1);
    26.         }
    27.  
     
    Tzelon and Rob-A like this.
  9. LezCT

    LezCT

    Joined:
    Nov 15, 2014
    Posts:
    55
    Can confirm this bug, mine only works I set the tap count to 1, and can of course, tap once to trigger something. otherwise, double-tap and above won't work.
    tested on 1.1.0-preview.2
     
  10. kasuyakema

    kasuyakema

    Joined:
    Sep 13, 2020
    Posts:
    11
  11. kasuyakema

    kasuyakema

    Joined:
    Sep 13, 2020
    Posts:
    11
    @LezTusi looks fixed to me. You can see in the log how the multitap gets cancelled and the tap is started instead:
    multitap.png
     
    LezCT likes this.
  12. Hellhound_01

    Hellhound_01

    Joined:
    Mar 5, 2016
    Posts:
    102
    Using Unity 2020.1.17f1 with InputSystem 1.1.0-preview2 and it is not working for me. I've this move action setup, which is called two times if I perform a double click. I also tried to change the interaction order of tab and multi-tap, but this results in same behaviour.

    Action binding is performed via script, while I also figured out that the PlayerInput component is buggy. If I use the PlayerInput component, the callback method is called 3 times (I think one for each state (down, press, up)), also without double-click behavior.

    Can anyone confirm this?

    upload_2020-12-16_8-4-40.png

    BTW: I can not figure out, how I could check if tab or multitab has been occured if my callback has been invoked is this possible without breaking the encapsulation, while checking the mouse input?
     
    Last edited: Dec 16, 2020
  13. LezCT

    LezCT

    Joined:
    Nov 15, 2014
    Posts:
    55
    My bad, i forgot i was using
    WasPressedThisFrame()
    on my input.
    using
    triggered
    works!
     
  14. LezCT

    LezCT

    Joined:
    Nov 15, 2014
    Posts:
    55
    using
    Input.actions["Fire"].triggered
    like the legacy input.getbutton works for me.
     
  15. Hellhound_01

    Hellhound_01

    Joined:
    Mar 5, 2016
    Posts:
    102
    @LezTusi Thanks for the hint, but i'm not shure what you mean ;)

    Meanwhile I got the click handling working. I've extended my OnMove callback by using the CallbackContext parameter. Now I could check which Interaction has triggered the action and could it handle separately:

    Code (CSharp):
    1. public void OnMove(InputAction.CallbackContext ctx){
    2.         Debug.Log("OnMove callback invoked! ");
    3.  
    4.         if (ctx.interaction is TapInteraction) Debug.Log("Single tap trigger!");
    5.         else if (ctx.interaction is MultiTapInteraction) Debug.Log("Multitap trigger!");
    6.     }
    Using this setup:

    upload_2020-12-16_13-55-54.png

    What I've figured out that the order of the interactions is strictly while those are executed from top down. If I use this set-up first the MultiMapInteraction is checked, if this interaction triggers the action, the other are not executed, if not, the next interaction (TapInteraction in my case) is executed and so on ...

    Not sure if this is an expected behaviour or a bug. I think it is a bug, I do not expect this behaviour and it is not documented. This also breaks the encapsulation, while I check the set up of a single action explicitly that was never in the mind of the inventor.
     

    Attached Files:

    Last edited: Dec 16, 2020
    Pnvanol and Tethip like this.
  16. kasuyakema

    kasuyakema

    Joined:
    Sep 13, 2020
    Posts:
    11
    @Hellhound_01 i think id did not work in screenshot 1 because the tap interaction was on top of the multitap interaction. That way the successfull tap interaction "consumes" the input. If the multitap is on top like in the second screenshot it works because a failed multitap passes on the input to the tap interaction.
     
  17. knight2fox

    knight2fox

    Joined:
    May 3, 2014
    Posts:
    17
    i need to have a tap interaction and a multi tap doing different things how do i achieve that

    either its single tap and one function should be executed or its multi tap and another action should be triggered but right now both the actions are getting triggered
     
  18. apexape

    apexape

    Joined:
    Sep 30, 2016
    Posts:
    3
    Is this still a bug? I've been having the same issues.

    update
    Code (CSharp):
    1.         public void DoubleClick(InputAction.CallbackContext context)
    2.         {
    3.             if (context.performed)
    4.             {
    5.                 Debug.Log("Action performed.");  
    6.             }
    7.         }
    8.  
    So using this code i got the correct behavior.
     
    Last edited: Mar 7, 2022
  19. GravelFrog

    GravelFrog

    Joined:
    Jan 13, 2014
    Posts:
    2
    This somewhat works, but I am seeing that holding the button down also triggered the Tap action. I would expect it would not.
     
    Pnvanol likes this.
  20. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    114
    when I press the button once, I get a double tap calling the function twice...
    I cant really find a way to ask for a double tap using the new input system