Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit 634b60d251288d81e772b5c21326d60d76bf8056 parent 29525b91887d0cdef5b2b85c97b941e11bdd72bc Author: sentomk <mk20040924@outlook.com> Date: Fri, 20 Nov 2026 21:41:09 +0800 feat: track held keys and pending presses Diffstat:
| A | src/platform/keyboard.h | | | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | tests/unit/keyboard_test.cpp | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
2 files changed, 99 insertions(+), 0 deletions(-)
diff --git a/src/platform/keyboard.h b/src/platform/keyboard.h @@ -0,0 +1,60 @@ +/// @file platform/keyboard.h +/// @brief Tracks held keys and pending key presses. +#pragma once + +#include <array> +#include <limits> + +#include "core/types.h" + +namespace kumo { + +/// @brief Keys tracked by Keyboard. Count is a sentinel. +enum class Key : uint8_t { Left, Right, Up, Down, Space, Count }; + +/// @brief Stores the current key state and presses waiting for a tick. +/// +/// A press stays pending even if the key is released before the next tick. +/// Key arguments must be one of Left, Right, Up, Down, or Space. +class Keyboard { + public: + /// @brief Updates a key and records a press when it goes from up to down. + void set_key(Key key, bool down) { + State& state = keys_[static_cast<size_t>(key)]; + if (down && !state.down && + state.presses < std::numeric_limits<uint32_t>::max()) { + ++state.presses; + } + state.down = down; + } + + /// @brief Returns whether the key is currently held. + bool is_down(Key key) const { return keys_[static_cast<size_t>(key)].down; } + + /// @brief Returns and clears the pending presses for the key. + uint32_t take_presses(Key key) { + State& state = keys_[static_cast<size_t>(key)]; + const uint32_t presses = state.presses; + state.presses = 0; + return presses; + } + + /// @brief Releases all held keys and keeps pending presses. + /// + /// Call when the window loses keyboard focus. + void release_all() { + for (State& state : keys_) { + state.down = false; + } + } + + private: + struct State { + bool down = false; + uint32_t presses = 0; + }; + + std::array<State, static_cast<size_t>(Key::Count)> keys_{}; +}; + +} // namespace kumo diff --git a/tests/unit/keyboard_test.cpp b/tests/unit/keyboard_test.cpp @@ -0,0 +1,39 @@ +#include "platform/keyboard.h" + +#include "check.h" + +using kumo::Key; +using kumo::Keyboard; + +TEST(quick_taps_survive_until_consumed) { + Keyboard keyboard; + keyboard.set_key(Key::Space, true); + keyboard.set_key(Key::Space, false); + keyboard.set_key(Key::Space, true); + keyboard.set_key(Key::Space, false); + + CHECK(!keyboard.is_down(Key::Space)); + CHECK_EQ(keyboard.take_presses(Key::Space), 2u); + CHECK_EQ(keyboard.take_presses(Key::Space), 0u); +} + +TEST(repeated_key_down_does_not_create_another_press) { + Keyboard keyboard; + keyboard.set_key(Key::Space, true); + CHECK_EQ(keyboard.take_presses(Key::Space), 1u); + + keyboard.set_key(Key::Space, true); + CHECK(keyboard.is_down(Key::Space)); + CHECK_EQ(keyboard.take_presses(Key::Space), 0u); +} + +TEST(focus_loss_releases_keys_and_preserves_pending_presses) { + Keyboard keyboard; + keyboard.set_key(Key::Left, true); + keyboard.set_key(Key::Space, true); + keyboard.release_all(); + + CHECK(!keyboard.is_down(Key::Left)); + CHECK(!keyboard.is_down(Key::Space)); + CHECK_EQ(keyboard.take_presses(Key::Space), 1u); +}
