The Input class provides a centralized system for managing mouse and keyboard input events. It maintains a global InputState that can be accessed by widgets and other components for input handling.
The Input class is typically used by platform-specific event handlers to update the input state, while widgets access the state for input processing.
Static Methods
getState()
static InputState& getState()
Returns a reference to the current global input state containing all mouse and keyboard information for this frame.
Reference to the current input state
const InputState& input = Input::getState();
if (input.mouseClicked) {
handleMouseClick(input.mouseX, input.mouseY);
}
resetEvents()
static void resetEvents()
Clears events that should only last one frame (like clicks and key presses). This should be called at the beginning of each frame by the main loop.
This preserves continuous states like mouse position and held keys.
updateMousePosition()
static void updateMousePosition(int x, int y)
Updates the global mouse coordinates. This should be called from platform-specific mouse move event handlers.
static void updateMouseButton(bool down)
Updates the mouse button press/release state. This should be called from platform-specific mouse button event handlers.
True if button is pressed, false if released
This automatically sets the mouseClicked flag for press events.
updateKeyPress()
static void updateKeyPress(KeyCode key)
Records a key press event. This should be called from platform-specific key press event handlers.
This automatically updates the keyPressed flag and lastKeyPressed.
updateKeyRelease()
static void updateKeyRelease(KeyCode key)
Records a key release event. This should be called from platform-specific key release event handlers.
The key that was released
This automatically updates the keyReleased flag and lastKeyReleased.
updateTextInput()
static void updateTextInput(const std::string& text)
Records text input for text fields and typing. This should be called from platform-specific text input event handlers.
text
const std::string&
required
The text that was entered (usually a single character)
This is separate from key presses and handles composed characters.
The InputState struct contains all current input information:
struct InputState {
// Mouse input
int mouseX = 0; // Current mouse X position
int mouseY = 0; // Current mouse Y position
bool mouseDown = false; // True if mouse button is currently pressed
bool mouseClicked = false; // True if mouse was clicked this frame
// Keyboard input
KeyCode lastKeyPressed = KeyCode::None; // Last key that was pressed
KeyCode lastKeyReleased = KeyCode::None; // Last key that was released
bool keyPressed = false; // True if any key was pressed this frame
bool keyReleased = false; // True if any key was released this frame
// Text input
std::string textInput = ""; // Text entered this frame
bool hasTextInput = false; // True if text was entered this frame
// Methods
bool isKeyDown(KeyCode key) const;
bool isKeyJustPressed(KeyCode key) const;
bool isKeyJustReleased(KeyCode key) const;
};
Example Usage
// In platform-specific mouse event handler:
void onMouseMove(int x, int y) {
Input::updateMousePosition(x, y);
}
void onMouseClick(bool down) {
Input::updateMouseButton(down);
}
// In widget input handling:
bool MyWidget::handleInput(const InputState& input) {
if (input.mouseClicked && contains(input.mouseX, input.mouseY)) {
onClick();
return true;
}
return false;
}