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?


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.