How to detect/handle right-click?

I am using 5.1.3 and all the plumbing is already in the editor to handle OnClick events for the new UI elements. But how do I detect/handle right-click? Or even shift+click?

You can try this way (But maybe it will not work):

Input.GetMouseButtonUp(0) - on up Left click
Input.GetMouseButtonUp(1) - on up Right click
or
Input.GetMouseButton(1) - when button is pressed

    public void OnMouseClick()
    {
        if (Input.GetMouseButtonUp(1))
        {
              // On Click Right button
        }

        if (Input.GetMouseButtonUp(1) && Input.GetKeyDown(KeyCode.LeftShift))
        {
              // On Click Right button + Left Shift
        }
    }
1 Like

The proper way for new GUI elements (or at least ONE of the proper ways!):
-Select the button and Add Component->Events->Event Trigger
-Add a Pointer Click in this event
-Create a new script on some object, and drop it in the box inside the event

An example script:

using UnityEngine;
using UnityEngine.EventSystems;

public class Test : MonoBehaviour
{
    public void Click(BaseEventData bed)
    {
        PointerEventData ped = (PointerEventData)bed;
        Debug.Log("Button: "+ped.pointerId);
    }
}

Select Test->Click from the dropdown in the event.

pointerId will be -1 for left mouse button, -2 for right and lower for further buttons. This will work nicely with buttons and other canvas elements.

To check for shift-click you can cheap out and look at regular input key codes.

2 Likes

@orb I’ve followed your advice and have got things working as desired. Thank you.

its work. thx