Search Unity

Question How to read additional mouse buttons' KeyCodes by Event ?

Discussion in 'Input System' started by kopf, Oct 27, 2020.

  1. kopf

    kopf

    Joined:
    Sep 1, 2017
    Posts:
    11
    Sorry if this is posted in the wrong section.

    I want to get KeyCodes by user input using Event.current (writing in C#).

    I have accomplished what I want for the most part by doing this:
    -checking the EventType of Event.current
    --for EventType.KeyDown, getting the KeyCode by keyCode directly
    --for EventType.MouseDown, getting the button id and determining the KeyCode.

    This is working, but I am struggling to read any input from custom/additional mouse buttons (i.e. forward/back buttons on the mouse, or mouse button ids that should be >2).

    How can I read these additional mouse buttons via Event.current (or any other way) ?

    Thanks for any help.
     
  2. kopf

    kopf

    Joined:
    Sep 1, 2017
    Posts:
    11
    To make this more clear: when Event.current.isMouse == true, Event.current.button will only return 0,1, or 2 (left, right, or middle click). It never recognizes a Mouse3 or 4 button.

    Does anyone know why this would be happening?
     
  3. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    In general, the code *actually* only supports 3 buttons. The code here is platform-dependent so could be there's some platform that actually does go beyond three buttons but at least on Windows, only the left, right, and middle mouse button are handled.
     
    kopf likes this.
  4. kopf

    kopf

    Joined:
    Sep 1, 2017
    Posts:
    11
    Ok thanks for the info; so basically Event seems limited in this situation.

    I've worked around my problem for now by checking Input.GetKeyDown for Mouse3-6 from an Update function; if anyone has other ideas that wouldn't require the Update function let me know, but this works for now.
     
  5. peaj_metric

    peaj_metric

    Joined:
    Sep 15, 2014
    Posts:
    146
    This is a pretty strange limitation considering Input.GetKey seems to work with up to 6 mouse buttons at runtime and has to support way more platforms (havent checked if it works on consoles and mobile ;))
    As far as I know the editor is limited to Windows, Mac and Linux.
    Besides the Input.GetKey works in playmode in the editor. So there has to be a way to get them in an editor window!?

    For now I will try to work around it with user32.dll:
    Code (CSharp):
    1. [DllImport("user32.dll")]
    2. public static extern Int16 GetAsyncKeyState(Int32 virtualKeyCode);
    Btw. it would be great to add mouse support to the shortcut manager.
     
    INeatFreak likes this.