Class ButtonControl
An axis that has a trigger point beyond which it is considered to be pressed.
Inherited Members
Namespace: UnityEngine .InputSystem .Controls
Assembly: Unity.InputSystem.dll
Syntax
public class ButtonControl : AxisControl
Remarks
By default, stored as a single bit. In that format, buttons will only yield 0
and 1 as values. However, buttons return are Axis
Constructors
ButtonControl()
Default-initialize the button control.
Declaration
public ButtonControl()
Remarks
The default format for the button control is Format
Examples
using UnityEngine;
using UnityEngine.InputSystem.Controls;
public class ButtonControlExample : MonoBehaviour
{
void Start()
{
var myButton = new ButtonControl();
}
//...
}
See Also
Fields
pressPoint
The minimum value the button has to reach for it to be considered pressed.
Declaration
public float pressPoint
Field Value
Type | Description |
---|---|
float |
Remarks
The button is considered pressed, if it has a value equal to or greater than this value.
By default, this property is set to -1. If the value of the property is negative,
default
The value can be configured as a parameter in a layout.
using UnityEngine;
using UnityEngine.InputSystem.Controls;
public class MyDevice : InputDevice
{
[InputControl(parameters = "pressPoint=0.234")]
public ButtonControl button { get; private set; }
//...
}
Properties
isPressed
Whether the button is currently pressed.
Declaration
public bool isPressed { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
A button is considered pressed if its value is equal to or greater
than its button press threshold (press
Examples
You can use this to read whether specific keys are currently pressed by using isPressed on keys, as shown in the following examples:
// Using KeyControl property directly.
Keyboard.current.spaceKey.isPressed
Keyboard.current.aKey.isPressed // etc.
// Using Key enum.
Keyboard.current[Key.Space].isPressed
// Using key name.
((KeyControl)Keyboard.current["space"]).isPressed
Note: The Input System identifies keys by physical layout, not according to the current language mapping of the keyboard. To query the name of the key according to the language mapping, use display
You can also use this to read mouse buttons, as shown in the following examples:
bool leftPressed = Mouse.current.leftButton.isPressed;
bool rightPressed = Mouse.current.rightButton.isPressed;
bool middlePressed = Mouse.current.middleButton.isPressed;
You can also check through all numbered buttons on the mouse: (this example does not cause allocations)
var controls = Mouse.current.allControls;
for (var i = 0; i < controls.Count; ++i)
{
var button = controls[i] as ButtonControl;
if (button != null && button.isPressed)
{
// respond to mouse button press here...
}
}
Or you can look up controls by name, like this:
bool leftPressed = ((ButtonControl)Mouse.current["leftButton"]).isPressed;
pressPointOrDefault
Return press
Declaration
public float pressPointOrDefault { get; }
Property Value
Type | Description |
---|---|
float |
wasPressedThisFrame
Whether the press started this frame.
Declaration
public bool wasPressedThisFrame { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
The first time this function - or wasReleasedThisFrame - are called, it's possible that extremely fast
inputs (or very slow frame update times) will result in presses/releases being missed.
Following the next input system update after either have been called, and from then on until the device is
destroyed, this ceases to be an issue.
// An example showing the use of this property on a gamepad button and a keyboard key.
using UnityEngine;
using UnityEngine.InputSystem;
public class ExampleScript : MonoBehaviour
{
void Update()
{
bool buttonPressed = Gamepad.current.aButton.wasPressedThisFrame;
bool spaceKeyPressed = Keyboard.current.spaceKey.wasPressedThisFrame;
}
}
_Note_: The Input System identifies keys by physical layout, not according to the current language mapping of the keyboard. To query the name of the key according to the language mapping, use displayYou can also use this property to read mouse buttons. For example:
Mouse.current.leftButton.wasPressedThisFrame Mouse.current.rightButton.wasPressedThisFrame Mouse.current.middleButton.wasPressedThisFrame
wasReleasedThisFrame
Whether the press ended this frame.
Declaration
public bool wasReleasedThisFrame { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
Note: The Input System identifies keys by physical layout, not according to the current language mapping of the keyboard. To query the name of the key according to the language mapping, use display
Examples
An example showing the use of this property on a gamepad button and a keyboard key:
using UnityEngine;
using UnityEngine.InputSystem;
public class ExampleScript : MonoBehaviour
{
void Update()
{
bool buttonReleased = Gamepad.current.aButton.wasReleasedThisFrame;
bool spaceKeyReleased = Keyboard.current.spaceKey.wasReleasedThisFrame;
}
}
Methods
IsValueConsideredPressed(float)
Whether the given value would be considered pressed for this button.
Declaration
public bool IsValueConsideredPressed(float value)
Parameters
Type | Name | Description |
---|---|---|
float | value | Value to check for if the button would be considered pressed or not. |
Returns
Type | Description |
---|---|
bool | True if |
Remarks
The default format for the control is Format
Examples
using UnityEngine;
using UnityEngine.InputSystem.Controls;
public class IsValueConsideredPressedExample : MonoBehaviour
{
void Start()
{
var myButton = new ButtonControl();
var valueToTest = 0.5f;
if (myButton.IsValueConsideredPressed(valueToTest))
{
Debug.Log("myButton is considered pressed at: " + valueToTest.ToString());
}
else
{
Debug.Log("myButton is not considered pressed at: " + valueToTest.ToString());
}
}
//...
}