I have switched my game to use the new Unity Input System, and I want to know which mouse button was pressed when an action is called.

Here's how it's set up: Input System

Whenever one of these three mouse buttons are pressed, the MouseButtonClicked action will fire. I want to somehow know which one of these buttons was pressed from this single action, rather than making 3 seperate actions for 3 seperate mouse buttons.

I have tried reading the values from InputAction.CallbackContext context but I can't seem to get a proper value from the mouse button which was pressed.

How can I do this?


1 Answer 1


You don't.

The InputActions assets in the new input system are meant to create an abstraction layer between different pieces of input hardware and what actions their inputs are supposed to trigger in the context of the game.

That means Actions are meant to be semantic, not technical. You don't want to do something when the player clicked the mouse button. You want to do something when the player delivered an input with a specific meaning within the context of your game. And one way to deliver that input might be the click of a specific mouse button... and another might be a gamepad button... and another a gesture on a touchscreen.

This philosophy is reflected by the InputValue object passed to your event handlers which react to those actions. It only provides a value and hides the technical details of which physical part of which device provided that value.

Currently you have an action with the meaning "Mouse Button Clicked" which is triggered by three different hardware inputs, namely left-click, right-click and middle-click. This would be fine if you wanted all three mouse buttons to do the same thing, but even then you would rename that action to what you actually want to happen on a mouse-click.

If you want each mouse-button to perform a different kind of action, then you would create three actions named after what you actually want to do (Select, Deselect, NavigateBack, MapPanningMode, Fire, AltFire... whatever makes sense in the context of your game), and assign each mouse button to the corresponding action.

Yes, the new input system has a very different philosophy than you might be used to as a user of the good old hardware-oriented Input class (especially when you are one of the Unity developers who so far eschewed the InputManager window and preferred to work with KeyCode's instead). So when you want to use the new input system efficiently, then you need to really change the way you think about input.

But it is really worth it to give it a chance, because when you understood and internalized the way it wants you to use it, then supporting different kinds of input devices and keyboard layouts turns from a chore into a triviality.

  • \$\begingroup\$ How would you handle a situation where knowing the type of input device determines how you handle the input? For example, moving a camera with the cursor keys or mouse "delta" is nicely device agnostic, right up until you only want the camera to move if due to mouse input when the mouse button is held down, but the cursor keys should work regardless of the mouse button. As far as I can tell, the only way to do this is to define separate InputActions for (mouse + mousebutton), and keyboard cursor keys, and special code to combine their control inputs, which means your code knows it's a mouse. \$\endgroup\$
    – davidA
    Commented Jun 16, 2021 at 4:57
  • \$\begingroup\$ @davidA Please ask that as a new question. \$\endgroup\$
    – Philipp
    Commented Jun 16, 2021 at 7:58
  • \$\begingroup\$ Sure: gamedev.stackexchange.com/questions/194100/… \$\endgroup\$
    – davidA
    Commented Jun 17, 2021 at 0:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .