Skip to content

Input

Keyboard, mouse, and game controller input handling.

Header: #include <asw/modules/input.h>Namespace: asw::input

Keyboard

Key Enum

The asw::input::Key enum maps to SDL scancodes. Common keys include:

KeyDescription
Key::A - Key::ZLetter keys
Key::Num0 - Key::Num9Number keys
Key::F1 - Key::F24Function keys
Key::ReturnEnter key
Key::EscapeEscape key
Key::SpaceSpacebar
Key::Up, Key::Down, Key::Left, Key::RightArrow keys
Key::LShift, Key::RShiftShift keys
Key::LCtrl, Key::RCtrlControl keys
Key::LAlt, Key::RAltAlt keys
Key::TabTab key
Key::BackspaceBackspace key

Keyboard Functions

getKey

cpp
bool getKey(asw::input::Key key);

Check if a key is currently held down.

getKeyDown

cpp
bool getKeyDown(asw::input::Key key);

Check if a key was pressed since the last update (single-tick).

getKeyUp

cpp
bool getKeyUp(asw::input::Key key);

Check if a key was released since the last update (single-tick).

KeyState

cpp
extern KeyState keyboard;

The global keyboard state. Holds arrays for pressed, released, and down states, plus anyPressed and lastPressed.

Mouse

MouseButton Enum

ValueDescription
MouseButton::LeftLeft mouse button
MouseButton::MiddleMiddle mouse button
MouseButton::RightRight mouse button
MouseButton::X1Extra button 1
MouseButton::X2Extra button 2

Mouse Functions

getMouseButton

cpp
bool getMouseButton(asw::input::MouseButton button);

Check if a mouse button is currently held down.

getMouseButtonDown

cpp
bool getMouseButtonDown(asw::input::MouseButton button);

Check if a mouse button was pressed since the last update.

getMouseButtonUp

cpp
bool getMouseButtonUp(asw::input::MouseButton button);

Check if a mouse button was released since the last update.

MouseState

cpp
extern MouseState mouse;

The global mouse state struct:

FieldTypeDescription
positionVec2<float>Current mouse position
zfloatScroll wheel value
xChangefloatHorizontal movement delta
yChangefloatVertical movement delta
anyPressedboolWhether any button is pressed
lastPressedintLast pressed button index

Cursor

setCursor

cpp
void setCursor(asw::input::CursorId cursor);

Change the system cursor. Available cursors include CursorId::Default, CursorId::Text, CursorId::Wait, CursorId::Crosshair, CursorId::Pointer, and various resize cursors.

Game Controller

Supports up to 8 game controllers simultaneously.

ControllerButton Enum

ValueDescription
ControllerButton::ASouth face button
ControllerButton::BEast face button
ControllerButton::XWest face button
ControllerButton::YNorth face button
ControllerButton::BackBack/Select button
ControllerButton::StartStart button
ControllerButton::GuideGuide/Home button
ControllerButton::LeftStickLeft stick press
ControllerButton::RightStickRight stick press
ControllerButton::LeftShoulderLeft bumper
ControllerButton::RightShoulderRight bumper
ControllerButton::DPadUp/Down/Left/RightD-pad directions

ControllerAxis Enum

ValueDescription
ControllerAxis::LeftXLeft stick X axis
ControllerAxis::LeftYLeft stick Y axis
ControllerAxis::RightXRight stick X axis
ControllerAxis::RightYRight stick Y axis
ControllerAxis::LeftTriggerLeft trigger
ControllerAxis::RightTriggerRight trigger

Controller Functions

getControllerButton

cpp
bool getControllerButton(int index, asw::input::ControllerButton button);

Check if a controller button is currently held down.

getControllerButtonDown

cpp
bool getControllerButtonDown(int index, asw::input::ControllerButton button);

Check if a controller button was pressed since the last update.

getControllerButtonUp

cpp
bool getControllerButtonUp(int index, asw::input::ControllerButton button);

Check if a controller button was released since the last update.

getControllerAxis

cpp
float getControllerAxis(int index, asw::input::ControllerAxis axis);

Get the value of a controller axis (between -1.0f and 1.0f).

setControllerDeadZone

cpp
void setControllerDeadZone(int index, float deadZone);

Set the joystick deadzone for a controller (default: 0.25f).

getControllerCount

cpp
int getControllerCount();

Get the number of connected controllers.

getControllerName

cpp
std::string getControllerName(int index);

Get the name of a controller.

Example

cpp
// Keyboard
if (asw::input::getKeyDown(asw::input::Key::Space)) {
  // jump
}

if (asw::input::getKey(asw::input::Key::A)) {
  // move left while held
}

// Mouse
if (asw::input::getMouseButtonDown(asw::input::MouseButton::Left)) {
  auto pos = asw::input::mouse.position;
  // handle click at pos
}

// Controller
float moveX = asw::input::getControllerAxis(0, asw::input::ControllerAxis::LeftX);
if (asw::input::getControllerButtonDown(0, asw::input::ControllerButton::A)) {
  // jump
}

Released under the MIT License.