API Input - shmellyorc/Box GitHub Wiki
InputMap
Namespace: Box.Inputs
Description
Represents a mapping of input controls for the game. Manages keyboard, mouse, and gamepad inputs, allowing you to bind named actions to combinations of keys, buttons, and axes, and query their states or axes values.
Constructor
public InputMap();
Initializes the input map by loading controller mappings from resources, updating gamepad connection states, and populating the initial gamepad state dictionary.
Properties
Property | Description |
---|---|
int Count |
Gets the number of defined input mappings. |
ActiveInputState ActiveState |
Gets the current active input device type (Keyboard, Mouse, or Gamepad). |
Vect2 GlobalMousePosition |
Gets the global mouse position relative to the monitor. |
Vect2 MousePosition |
Gets the mouse position relative to the game window. |
bool AnyGamepadConnected |
Indicates whether any gamepad is currently connected. |
Methods
Method Signature | Description | Returns |
---|---|---|
void Add(string name, params Enum[] keys) |
Adds a new named input mapping or appends keys to an existing one. | void |
void Add(Enum name, params Enum[] keys) |
Overload: uses an enum value as the mapping name. | void |
bool Exists(string name) |
Checks if an input mapping with the given name exists. | bool |
bool Exists(Enum name) |
Overload: checks existence using an enum name. | bool |
bool Remove(string name) |
Removes an entire input mapping by name. | bool |
bool Remove(Enum name) |
Overload: removes by enum name. | bool |
bool Remove(string name, Enum key) |
Removes a specific key from a mapping; deletes mapping if it becomes empty. | bool |
bool Remove(Enum name, Enum key) |
Overload: removes key from mapping using enum names. | bool |
void Clear() |
Clears all input mappings. | void |
bool IsActionReleased(string name) |
Returns true if the named action is in a released (up) state. | bool |
bool IsActionReleased(Enum name) |
Overload: checks release state using an enum name. | bool |
float GetActionAxisReleased(string left, string right) |
Returns -1 if left action released, 1 if right released, 0 otherwise. | float |
float GetActionAxisReleased(Enum left, Enum right) |
Overload: axis release check with enum names. | float |
bool IsActionPressed(string name) |
Returns true if the named action is currently pressed (down). | bool |
bool IsActionPressed(Enum name) |
Overload: checks pressed state using an enum name. | bool |
float GetActionAxisPressed(string left, string right) |
Returns -1 if left action pressed, 1 if right pressed, 0 otherwise. | float |
float GetActionAxisPressed(Enum left, Enum right) |
Overload: axis press check with enum names. | float |
bool IsActionJustPressed(string name) |
Returns true if the named action was pressed this frame. | bool |
bool IsActionJustPressed(Enum name) |
Overload: just-pressed check using an enum name. | bool |
float GetActionAxisJustPressed(string left, string right) |
Returns -1 if left action just pressed, 1 if right just pressed, 0 otherwise. | float |
float GetActionAxisJustPressed(Enum left, Enum right) |
Overload: just-pressed axis check with enum names. | float |
bool IsKeyPressed(KeyboardButton button) |
Checks if a specific keyboard button is currently down. | bool |
float GetKeyAxisPressed(KeyboardButton left, KeyboardButton right) |
Returns -1, 1, or 0 based on keyboard keys held down. | float |
bool IsKeyReleased(KeyboardButton button) |
Checks if a keyboard button is currently up. | bool |
float GetKeyAxisReleased(KeyboardButton left, KeyboardButton right) |
Returns -1, 1, or 0 based on keyboard keys released. | float |
bool IsKeyJustPressed(KeyboardButton button) |
Checks if a keyboard button was pressed this frame. | bool |
float GetKeyAxisJustPressed(KeyboardButton left, KeyboardButton right) |
Returns -1, 1, or 0 based on first keyboard press. | float |
bool IsMousePressed(MouseButton button) |
Checks if a mouse button is currently down. | bool |
bool IsMouseReleased(MouseButton button) |
Checks if a mouse button is currently up. | bool |
bool IsMouseJustPressed(MouseButton button) |
Checks if a mouse button was pressed this frame. | bool |
bool IsGamepadReleased(GamepadButton button) |
Checks if a gamepad button is currently released. | bool |
float GetGamepadAxisReleased(GamepadButton left, GamepadButton right) |
Returns -1, 1, or 0 based on gamepad button release. | float |
bool IsGamepadJustPressed(GamepadButton button) |
Checks if a gamepad button was pressed this frame. | bool |
float GetGamepadAxisJustPressed(GamepadButton left, GamepadButton right) |
Returns -1, 1, or 0 based on first gamepad press. | float |
bool IsGamepadPressed(GamepadButton button) |
Checks if a gamepad button is down, including deadzone and mapping logic. | bool |
float GetGamepadAxisPressed(GamepadButton left, GamepadButton right) |
Returns -1, 1, or 0 based on gamepad buttons held down. | float |
float GetGamepadForce(GamepadButton button) |
Retrieves analog force (0.0 to 1.0) for triggers and thumbsticks. | float |
Examples
// Define action names
enum PlayerAction { MoveLeft, MoveRight, Jump }
// Create and configure InputMap
var inputMap = new InputMap();
inputMap.Add(PlayerAction.MoveLeft, KeyboardButton.A, GamepadButton.DpadLeft);
inputMap.Add(PlayerAction.MoveRight, KeyboardButton.D, GamepadButton.DpadRight);
inputMap.Add(PlayerAction.Jump, KeyboardButton.Space, GamepadButton.A);
// In your game update loop:
if (inputMap.IsActionJustPressed(PlayerAction.Jump))
{
player.Jump();
}
float horizontal = inputMap.GetActionAxisPressed(PlayerAction.MoveLeft, PlayerAction.MoveRight);
player.Move(horizontal);