# How do I…? Devices: * […check if the space key has been pressed this frame?](#check-if-the-space-key-has-been-pressed-this-frame) * […find all connected gamepads?](#find-all-connected-gamepads) * […find the gamepad that the player is currently using?](#find-the-gamepad-that-the-player-is-currently-using) * […know when a new device has been plugged in?](#know-when-a-new-device-has-been-plugged-in) Actions: * […create a simple Fire-type Action?](#create-a-simple-fire-type-action) * […require a button to be held for 0.4 seconds before triggering an Action?](#require-a-button-to-be-held-for-04-seconds-before-triggering-an-action) * […use a "positive" and a "negative" button to drive an axis?](#use-a-positive-and-a-negative-button-to-drive-an-axis) * […create a UI to rebind input in my game?](#create-a-ui-to-rebind-input-in-my-game) ## …check if the space key has been pressed this frame? Use this code: ```CSharp Keyboard.current.space.wasPressedThisFrame ``` You can adapt this code to other Devices that have buttons or other types of input: ```CSharp Gamepad.current.aButton.wasPressedThisFrame ``` ## …find all connected gamepads? Use the [`Gamepad`](../api/UnityEngine.InputSystem.Gamepad.html) class: ```CSharp var allGamepads = Gamepad.all; // Or more specific versions. var allPS4Gamepads = DualShockGamepadPS4.all; ``` Alternatively, you can use more generic Device queries using LINQ expressions or [control paths](Controls.md#control-paths): ```CSharp // Go through all devices and select gamepads. InputSystem.devices.Select(x => x is Gamepad); // Query everything that is using the gamepad template or based on that template. // NOTE: Don't forget to Dispose() the result. InputSystem.FindControls(""); ``` ## …find the gamepad that the player is currently using? Use this code: ```CSharp var gamepad = Gamepad.current; // This works for other types of devices, too. var keyboard = Keyboard.current; var mouse = Mouse.current; ``` # …know when a new Device has been plugged in? Use this code: ```CSharp InputSystem.onDeviceChange += (device, change) => { switch (change) { case InputDeviceChange.Added: // New Device. break; case InputDeviceChange.Disconnected: // Device got unplugged. break; case InputDeviceChange.Connected: // Plugged back in. break; case InputDeviceChange.Removed: // Remove from Input System entirely; by default, Devices stay in the system once discovered. break; default: // See InputDeviceChange reference for other event types. break; } } ``` For more details, see documentation on ["Monitoring Devices"](Devices.md#monitoring-devices). # …create a simple Fire-type Action? Use this code: ```C# // Create an Action that binds to the primary action control on all devices. var action = new InputAction(binding: "*/{primaryAction}"); // Have it run your code when the Action is triggered. action.performed += _ => Fire(); // Start listening for control changes. action.Enable(); ``` You can also have a serialized field of type `InputAction` in your `MonoBehaviour`, like this: ```C# public class MyControllerComponent : MonoBehaviour { public InputAction fireAction; public InputAction walkAction; } ``` The Editor lets you add and edit Bindings for the Actions in the Inspector window. >__Note__: You still need to enable the Action in code and hook up your response. You can do so in the `Awake` method of your component: ```C# void Awake() { fireAction.performed += Fire; walkAction.performed += Walk; } void OnEnable() { fireAction.Enable(); walkAction.Enable(); } void OnDisable() { fireAction.Disable(); walkAction.Disable(); } void Fire(CallbackContext ctx) { //... } void Walk(CallbackContext ctx) { //... } ``` If you're worried about allocations from the delegates, you can use a polling approach rather than a callback-driven approach. ```C# void OnEnable() { fireAction.Enable(); walkAction.Enable(); } void OnDisable() { fireAction.Disable(); walkAction.Disable(); } void Update() { if (fireAction.triggered) Fire(); if (walkAction.triggered) Walk(); } void Fire() { //... } void Walk() { //... } ``` Typically, you need to deal with more than one Action. In this case, you might want to put these into an Input Actions Asset file. To create an Input Actions Asset, follow these steps: 1. In the Project view, click __Create__. 2. Select __Input Actions__. 3. Double-click the Asset to create and edit one or multiple Input Action Maps containing Input Actions. If you then click __Generate C# Class__ in the Inspector for that Asset, Unity generates a wrapper class for your Actions which you can use like this: ```C# MyInputActionAssetClass actions; public void OnEnable() { actions = new MyInputActionAssetClass(); controls.myActionsMap.fire.performed += Fire; controls.myActionsMap.walk.performed += Walk; } ``` For more details, see documentation on [Actions](Actions.md). # …require a button to be held for 0.4 seconds before triggering an Action? Put a [hold Interaction](Interactions.md#hold) on the Action, like this: ```C# var action = new InputAction(binding: "*/{PrimaryAction}", modifiers: "hold(duration=0.4)"); ``` To display UI feedback when the button starts being held, use the [`started`](../api/UnityEngine.InputSystem.InputAction.html#UnityEngine_InputSystem_InputAction_started) callback. ```C# action.started += _ => ShowGunChargeUI(); action.performed += _ => FinishGunChargingAndHideChargeUI(); action.cancelled += _ => HideChargeUI(); ``` # …use a "positive" and a "negative" button to drive an axis? Use an [axis composite](ActionBindings.md#1d-axis) binding in the UI or in code, and bind its parts to the respective buttons. ```CSharp var accelerateAction = new InputAction("Accelerate"); accelerateAction.AddCompositeBinding("Axis") .With("Positive", "/rightTrigger") .With("Negative", "/leftTrigger"); ``` # …create a UI to rebind input in my game? Create a UI with a button to trigger rebinding. If the user clicks the button to bind a control to an action, use [`InputAction.PerformInteractiveRebinding`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_PerformInteractiveRebinding_UnityEngine_InputSystem_InputAction_System_Int32_) to handle the rebinding: ```C# void RemapButtonClicked(InputAction actionToRebind) { var rebindOperation = actionToRebind.PerformInteractiveRebinding() // To avoid accidental input from mouse motion .WithControlsExcluding("Mouse") .OnMatchWaitForAnother(0.1f) .Start(); } ``` You can install the Tanks Demo sample for the Input System package using the Package Manager, which has an example of an interactive rebinding UI. # …set up an Action to specifically target the left-hand XR controller? Use this code: ```CSharp var action = new InputAction(binding: "/{leftHand}/position"); ``` You can also set this up for any Input Bindings in the Inspector or the Input Action Asset editor window without having to deal with paths directly. # …wait for any button to be pressed on any device? [//]: # (TODO this needs work) Use this code: ```CSharp var myAction = new InputAction(binding: "/*/