Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by PaperMouseGames, Jul 15, 2020.

  1. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    Hi there, so I've been using the following code to get info about what the player is clicking on:

    Code (CSharp):
    1. public class ClickableObject : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
    2. {
    3. public void OnPointerClick(PointerEventData eventData)
    4.    {
    5.         if (eventData.button == PointerEventData.InputButton.Left)
    6.         {
    7.            // do something...
    8.         }
    9.    }
    10. }
    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:

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(1) && Input.GetKeyDown(KeyCode.LeftShift))
    2.         {
    3.               // do something
    4.         }
    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.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    Couldnt you just integrate the GetKey(Shift) check into the OnPointerClick method? Like so:

    Code (CSharp):
    1. public void OnPointerClick(PointerEventData eventData)
    2. {
    3.      if (eventData.button == PointerEventData.InputButton.Left && Input.GetKey(KeyCode.LeftShift))
    4.      {
    5.         // do something...
    6.      }
    7. }
    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.
     
  3. PaperMouseGames

    PaperMouseGames

    Joined:
    Jul 31, 2018
    Posts:
    434
    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:

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.LeftShift) && Input.GetMouseButtonDown(1))
    2.         {
    3.               // do something
    4.         }
     
    Yoreki likes this.