Input class in Thread
Static polling API for keyboard / mouse / gamepad state. Call it from
OnUpdate. The engine snapshots every device once per frame, before any
script runs, so all systems in a frame observe the same input and the
Was… edge queries are exact.
The header core/Input.h pulls in no windowing backend — game code sees
only the Thread::Key, Thread::Mouse,
Thread::GamepadButton and Thread::GamepadAxis enums, never a
GLFW symbol.
Keyboard
| IsKeyPressed(Key) | bool | True while the key is held. |
| WasKeyPressed(Key) | bool | True only on the frame the key went down. |
| WasKeyReleased(Key) | bool | True only on the frame the key came up. |
Mouse
| IsMouseButtonPressed(Mouse) | bool | True while held. |
| WasMouseButtonPressed(Mouse) | bool | Button-down edge. |
| WasMouseButtonReleased(Mouse) | bool | Button-up edge. |
| GetMousePosition() | pair<float,float> | Window-space pixel coordinates. |
| GetMouseDelta() | pair<float,float> | Movement since the previous frame. Reads zero across cursor warps, cursor-mode changes and alt-tab, so mouse-look never jumps. |
| GetMouseScroll() | pair<float,float> | Wheel movement accumulated this frame (y is the usual wheel). |
| SetMousePosition(x, y) | void | Warp the cursor. |
| SetCursorMode(CursorMode) | void | Normal, Hidden or Locked (captured, for mouse-look). |
| ShowCursor(bool) | void | Shorthand: true → Normal, false → Locked. |
| InvalidateMouseDelta() | void | Forget the remembered cursor position, so the next frame's delta reads zero. |
You almost never need to guard the delta yourself. Warps, cursor-mode changes and
regaining window focus all drop the remembered position for you, so a controller can read
GetMouseDelta() on the very first frame without seeding anything. Re-implementing a
“skip the first frame” check on top of that is the usual way to introduce a stutter rather than
remove one.
InvalidateMouseDelta() exists for the case the engine cannot see: something outside it
moved the pointer. Call it before the next frame is sampled — the engine does exactly that on
the focus edge, above Input::Update() in the main loop. Calling it later in the frame
silences the frame after the one that needed it, which is no help at all.
Gamepad
Indices 0 .. Input::MaxGamepads-1. Sticks and triggers report
[-1, 1]; SetGamepadDeadzone() controls the threshold the action
mapping uses to treat an axis as active.
| IsGamepadConnected(pad = 0) | bool | A mapped controller is present. |
| IsGamepadButtonPressed(GamepadButton, pad = 0) | bool | True while held. |
| WasGamepadButtonPressed(GamepadButton, pad = 0) | bool | Button-down edge. |
| WasGamepadButtonReleased(GamepadButton, pad = 0) | bool | Button-up edge. |
| GetGamepadAxis(GamepadAxis, pad = 0) | float | Raw axis value. |
Action mapping
Bind a named action to any mix of keys, mouse buttons, gamepad buttons and gamepad
axes, then query it by name — the gameplay code never mentions a device. scale
is the signed weight of a binding, so -1/+1 on opposite keys
builds a movement axis.
| BindActionKey(name, Key, scale = 1) | void | Add a key binding. |
| BindActionMouse(name, Mouse, scale = 1) | void | Add a mouse-button binding. |
| BindActionGamepadButton(name, GamepadButton, scale = 1, pad = 0) | void | Add a gamepad-button binding. |
| BindActionAxis(name, GamepadAxis, scale = 1, pad = 0) | void | Add a gamepad-axis binding. |
| ClearAction(name) / ClearAllActions() | void | Drop bindings (rebinding). |
| IsActionPressed(name) | bool | Any binding active. |
| WasActionPressed(name) | bool | Action went active this frame. |
| WasActionReleased(name) | bool | Action went inactive this frame. |
| GetActionValue(name) | float | Signed sum of active contributions, clamped to [-1, 1]. |
Display names
GetKeyName(), GetMouseName(),
GetGamepadButtonName() and GetGamepadAxisName() return a stable
ASCII label ("Left Shift", "D-Pad Up", …) for rebinding menus and
on-screen key prompts. They never return null.
Key
Thread::Key: A..Z, D0..D9, F1..F25,
KP0..KP9, Space, Enter, Escape,
LeftShift, LeftControl, arrow keys, etc.
Thread::Mouse: Left, Right, Middle,
Button3..Button7.
Example
// Once, at startup — WASD or the left stick, whichever the player uses.
Thread::Input::BindActionKey("MoveX", Thread::Key::D, +1.0f);
Thread::Input::BindActionKey("MoveX", Thread::Key::A, -1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::S, +1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::W, -1.0f);
Thread::Input::BindActionAxis("MoveX", Thread::GamepadAxis::LeftX);
Thread::Input::BindActionAxis("MoveZ", Thread::GamepadAxis::LeftY);
Thread::Input::BindActionKey("Jump", Thread::Key::Space);
Thread::Input::BindActionGamepadButton("Jump", Thread::GamepadButton::A);
void OnUpdate(float ts) override {
auto& tc = GetComponent<Thread::TransformComponent>();
const float speed = 4.0f;
tc.Translation.x += Thread::Input::GetActionValue("MoveX") * speed * ts;
tc.Translation.z += Thread::Input::GetActionValue("MoveZ") * speed * ts;
// Fires once per press, on either the key or the pad.
if (Thread::Input::WasActionPressed("Jump")) Jump();
// Mouse-look: the delta is already zeroed across cursor warps.
auto [dx, dy] = Thread::Input::GetMouseDelta();
m_Yaw -= dx * 0.0022f;
}
Input klasa w Thread
Statyczne API odpytywania stanu klawiatury, myszy i pada. Wołaj je z OnUpdate. Silnik
robi migawkę każdego urządzenia raz na klatkę, zanim wykona się jakikolwiek skrypt, więc wszystkie
systemy w klatce widzą to samo wejście, a zapytania o zbocza Was… są dokładne.
Nagłówek core/Input.h nie wciąga żadnego backendu okien — kod gry widzi tylko enumy
Thread::Key, Thread::Mouse, Thread::GamepadButton i
Thread::GamepadAxis, nigdy symbolu GLFW.
Klawiatura
| IsKeyPressed(Key) | bool | True, dopóki klawisz jest wciśnięty. |
| WasKeyPressed(Key) | bool | True tylko w klatce, w której klawisz został wciśnięty. |
| WasKeyReleased(Key) | bool | True tylko w klatce, w której klawisz został puszczony. |
Mysz
| IsMouseButtonPressed(Mouse) | bool | True, dopóki wciśnięty. |
| WasMouseButtonPressed(Mouse) | bool | Zbocze wciśnięcia. |
| WasMouseButtonReleased(Mouse) | bool | Zbocze puszczenia. |
| GetMousePosition() | pair<float,float> | Współrzędne w pikselach okna. |
| GetMouseDelta() | pair<float,float> | Ruch od poprzedniej klatki. Daje zero przy przeskokach kursora, zmianach trybu kursora i alt-tabie, więc rozglądanie myszą nigdy nie skacze. |
| GetMouseScroll() | pair<float,float> | Ruch kółka zebrany w tej klatce (y to zwykłe kółko). |
| SetMousePosition(x, y) | void | Przenosi kursor. |
| SetCursorMode(CursorMode) | void | Normal, Hidden albo Locked (przechwycony, do rozglądania). |
| ShowCursor(bool) | void | Skrót: true → Normal, false → Locked. |
| InvalidateMouseDelta() | void | Zapomina zapamiętaną pozycję kursora, więc delta w następnej klatce wynosi zero. |
Prawie nigdy nie musisz sam zabezpieczać delty. Przeskoki, zmiany trybu kursora i
odzyskanie fokusu okna same kasują zapamiętaną pozycję, więc kontroler może czytać
GetMouseDelta() już w pierwszej klatce bez inicjalizacji. Dopisanie własnego „pomiń
pierwszą klatkę” to zwykle sposób na wprowadzenie przycięcia, a nie jego usunięcie.
InvalidateMouseDelta() jest na przypadek, którego silnik nie widzi: coś poza nim
przesunęło wskaźnik. Wołaj to przed próbkowaniem następnej klatki — silnik robi dokładnie
tak przy zmianie fokusu, nad Input::Update() w pętli głównej. Wywołanie później w klatce
wycisza klatkę po tej, która tego potrzebowała, czyli nic nie daje.
Pad
Indeksy 0 .. Input::MaxGamepads-1. Gałki i spusty dają [-1, 1];
SetGamepadDeadzone() ustala próg, od którego mapowanie akcji uznaje oś za aktywną.
| IsGamepadConnected(pad = 0) | bool | Podłączony jest zmapowany kontroler. |
| IsGamepadButtonPressed(GamepadButton, pad = 0) | bool | True, dopóki wciśnięty. |
| WasGamepadButtonPressed(GamepadButton, pad = 0) | bool | Zbocze wciśnięcia. |
| WasGamepadButtonReleased(GamepadButton, pad = 0) | bool | Zbocze puszczenia. |
| GetGamepadAxis(GamepadAxis, pad = 0) | float | Surowa wartość osi. |
Mapowanie akcji
Przypisz nazwanej akcji dowolną mieszankę klawiszy, przycisków myszy, przycisków i osi pada, a
potem pytaj o nią po nazwie — kod gry nigdy nie wspomina urządzenia. scale to waga ze
znakiem, więc -1/+1 na przeciwnych klawiszach daje oś ruchu.
| BindActionKey(name, Key, scale = 1) | void | Dodaje przypisanie klawisza. |
| BindActionMouse(name, Mouse, scale = 1) | void | Dodaje przypisanie przycisku myszy. |
| BindActionGamepadButton(name, GamepadButton, scale = 1, pad = 0) | void | Dodaje przypisanie przycisku pada. |
| BindActionAxis(name, GamepadAxis, scale = 1, pad = 0) | void | Dodaje przypisanie osi pada. |
| ClearAction(name) / ClearAllActions() | void | Usuwa przypisania (zmiana klawiszy). |
| IsActionPressed(name) | bool | Któreś przypisanie jest aktywne. |
| WasActionPressed(name) | bool | Akcja stała się aktywna w tej klatce. |
| WasActionReleased(name) | bool | Akcja przestała być aktywna w tej klatce. |
| GetActionValue(name) | float | Suma aktywnych wkładów ze znakiem, przycięta do [-1, 1]. |
Nazwy do wyświetlania
GetKeyName(), GetMouseName(), GetGamepadButtonName() i
GetGamepadAxisName() zwracają stałą etykietę ASCII ("Left Shift",
"D-Pad Up", …) do menu zmiany klawiszy i podpowiedzi na ekranie. Nigdy nie zwracają
null.
Key
Thread::Key: A..Z, D0..D9, F1..F25,
KP0..KP9, Space, Enter, Escape,
LeftShift, LeftControl, strzałki itd.
Thread::Mouse: Left, Right, Middle,
Button3..Button7.
Przykład
// Raz, przy starcie — WASD albo lewa gałka, czego gracz używa.
Thread::Input::BindActionKey("MoveX", Thread::Key::D, +1.0f);
Thread::Input::BindActionKey("MoveX", Thread::Key::A, -1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::S, +1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::W, -1.0f);
Thread::Input::BindActionAxis("MoveX", Thread::GamepadAxis::LeftX);
Thread::Input::BindActionAxis("MoveZ", Thread::GamepadAxis::LeftY);
Thread::Input::BindActionKey("Jump", Thread::Key::Space);
Thread::Input::BindActionGamepadButton("Jump", Thread::GamepadButton::A);
void OnUpdate(float ts) override {
auto& tc = GetComponent<Thread::TransformComponent>();
const float speed = 4.0f;
tc.Translation.x += Thread::Input::GetActionValue("MoveX") * speed * ts;
tc.Translation.z += Thread::Input::GetActionValue("MoveZ") * speed * ts;
// Raz na wciśnięcie, z klawisza albo pada.
if (Thread::Input::WasActionPressed("Jump")) Jump();
// Rozglądanie myszą: delta jest już wyzerowana przy przeskokach kursora.
auto [dx, dy] = Thread::Input::GetMouseDelta();
m_Yaw -= dx * 0.0022f;
}