Add, change, and erase bindings in code
Add bindings to actions, change their properties, erase them, and assign them to control schemes from your scripts.
For a list of the properties of a binding, refer to Binding properties.
The examples on this page use the following variables:
playerInput: aPlayerInputcomponent.action: anInputAction.asset: theInputActionAssetthat contains the action.
Add bindings
To add a binding to an action, call AddBinding:
// Add a binding for the left mouse button to the "fire" action.
playerInput.actions["fire"].AddBinding("<Mouse>/leftButton");
To add a composite binding, use AddCompositeBinding. For more information, refer to Add composite bindings in code.
Change bindings
To change an existing binding, call ChangeBinding to get a binding accessor, then use the accessor's methods to change the binding's properties. These changes modify the binding itself. To change a binding temporarily, apply a binding override instead.
// Get an accessor for the second binding of the "fire" action.
var accessor = playerInput.actions["fire"].ChangeBinding(1);
// Change the binding's path to the space key.
accessor.WithPath("<Keyboard>/space");
// You can also get an accessor through the action map, which holds the bindings
// of all its actions. This gets the third binding in the "gameplay" map.
accessor = playerInput.actions.FindActionMap("gameplay").ChangeBinding(2);
To move the accessor to another binding, call PreviousBinding or NextBinding:
// Move the accessor to the previous binding.
accessor = accessor.PreviousBinding();
// Move the accessor to the next binding.
accessor = accessor.NextBinding();
To find a binding by its name instead of its index, pass the name to ChangeBinding. To find a composite binding by its name or its composite type, call ChangeCompositeBinding:
// Get the binding named "WASD" on the "move" action.
accessor = playerInput.actions["move"].ChangeBinding("WASD");
// Get the 2D vector composite binding on the "move" action.
accessor = playerInput.actions["move"].ChangeCompositeBinding("2DVector");
Erase bindings
Call Erase on a binding's accessor to erase the binding. When you erase a composite binding, the Input System also erases its parts. To erase a binding:
// Erase the first binding of the "fire" action.
playerInput.actions["fire"].ChangeBinding(0).Erase();
// Erase the 2D vector composite binding of the "move" action, and its parts.
playerInput.actions["move"].ChangeCompositeBinding("2DVector").Erase();
// Erase a composite binding by its name.
playerInput.actions["move"].ChangeCompositeBinding("WASD").Erase();
// Erase the first binding in the "gameplay" action map.
playerInput.actions.FindActionMap("gameplay").ChangeBinding(0).Erase();
Assign bindings to control schemes
Control schemes group bindings by the type of device they use, so that you can enable or disable each group. For example, you can enable all keyboard and mouse bindings when the user presses a key or moves the mouse.
A control scheme uses the binding groups in InputBinding.groups. To put a binding in a group, pass the group when you add the binding. To use only the bindings in some groups, set a binding mask on the action map or asset with InputActionMap.bindingMask or InputActionAsset.bindingMask.
To assign bindings to control schemes:
// Add one binding for each control scheme.
action.AddBinding("<Gamepad>/buttonSouth", groups: "Gamepad");
action.AddBinding("<Keyboard>/space", groups: "Keyboard&Mouse");
// Use only the bindings in the "Gamepad" group.
asset.bindingMask = InputBinding.MaskByGroup("Gamepad");
The PlayerInput component uses binding masks to enable the control scheme that matches the device of a user who joins.
Binding properties
Each InputBinding has the following properties.
| Property | Description | Example |
|---|---|---|
path |
The control path that identifies the controls the action receives input from. | "<Gamepad>/leftStick" |
overridePath |
A control path that replaces path without changing it. The Input System doesn't save overridePath, so you can use it to change a binding temporarily. If overridePath isn't null, it takes effect instead of path. To get the path that's in effect, read effectivePath. |
"<Gamepad>/rightStick" |
action |
The name or ID of the action that the binding triggers. It can be null or empty, for example, on a composite binding. Not case-sensitive. |
"fire" |
groups |
A semicolon-separated list of the binding groups the binding belongs to. It can be null or empty. Binding groups can be anything, but they're mostly used for control schemes. Not case-sensitive. |
"Keyboard&Mouse;Gamepad" |
interactions |
A comma-separated list of interactions to apply to input on this binding. The Input System adds any interactions on the action to this list. Not case-sensitive. | "slowTap,hold(duration=0.75)" |
processors |
A comma-separated list of processors to apply to input on this binding. The Input System adds any processors on the action to this list. Not case-sensitive. | "invert,axisDeadzone(min=0.1,max=0.95)" |
id |
The unique ID of the binding. For example, use it to identify the binding when you save binding overrides in user settings. | N/A |
name |
The optional name of the binding. On the parts of a composite binding, the name identifies the part. | "Positive" |
isComposite |
Whether the binding is a composite binding. | N/A |
isPartOfComposite |
Whether the binding is a part of a composite binding. | N/A |
To get a flat list of the bindings for all the actions in an action map, use InputActionMap.bindings.