Can you detect modifier keys (Shift, CTRL, etc) in conjunction with PointerEventData?

Hi there, so I’ve been using the following code to get info about what the player is clicking on:

public class ClickableObject : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
public void OnPointerClick(PointerEventData eventData)
   {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
           // do something...
        }
   }
}

But now I want to be able to get the same sort of information when the user does a shift+click or a CTRL+click on something.

I know how to get the input keys that a user is pressing. Something like this:

if (Input.GetMouseButtonDown(1) && Input.GetKeyDown(KeyCode.LeftShift))
        {
              // do something
        }

But I don’t know how to integrate this with the OnPointerClick method, or if it’s even possible.

A little more about what I’m doing: I’m basically doing this to get info on items when the player clicks on items in their inventory. Now I want to add a functionality to do something different with the item by shift clicking, but I’m using the IPointerClickHandler so that I can get information on what item is being clicked on.

Couldnt you just integrate the GetKey(Shift) check into the OnPointerClick method? Like so:

public void OnPointerClick(PointerEventData eventData)
{
     if (eventData.button == PointerEventData.InputButton.Left && Input.GetKey(KeyCode.LeftShift))
     {
        // do something...
     }
}

By the way, in your below code example both keys are checked for “down”. This is only true for one frame, and highly unlikely for two keys to occur on the same frame, even if the user presses them both. So in most cases you want to check for a down on one key, while another is being held down. So instead of “check if both left click and shift were pressed this frame” you’d “check if a left click is pressed while the shift key is down”. Hence above it would also be GetKey, not GetKeyDown.

4 Likes

First off, thanks for the reply!

So I was going to write a response about how I tried that but it wasn’t working for me, when I realized that I was putting that else if below the if of the single left click and so it was just never getting to it… woops haha silly mistake, but yes, it is working now, thank you!!!

As for your other point about my second example, yeah, you’re absolutely right, I wrote that one up quickly and so I wasn’t thinking about it, but yeah I guess you would want it to look more like this:

if (Input.GetKey(KeyCode.LeftShift) && Input.GetMouseButtonDown(1))
        {
              // do something
        }
1 Like