From f9c3363a26ffbd5bf549cb45cf86b93a0b93982f Mon Sep 17 00:00:00 2001 From: houmain Date: Sun, 14 Jul 2024 20:50:16 +0200 Subject: [PATCH] Prevent key state vaIidation from resetting virtual key state on Windows --- src/runtime/Key.h | 6 ++++++ src/runtime/Stage.cpp | 14 ++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/runtime/Key.h b/src/runtime/Key.h index 8025e35..3d95081 100644 --- a/src/runtime/Key.h +++ b/src/runtime/Key.h @@ -500,6 +500,12 @@ constexpr bool is_keyboard_key(Key key) { !is_mouse_wheel(key)); } +constexpr bool is_device_key(Key key) { + return (is_keyboard_key(key) || + is_mouse_button(key) || + is_mouse_wheel(key)); +} + constexpr bool is_action_key(Key key) { return (key >= Key::first_action && key <= Key::last_action); } diff --git a/src/runtime/Stage.cpp b/src/runtime/Stage.cpp index 496d03c..c74b86b 100644 --- a/src/runtime/Stage.cpp +++ b/src/runtime/Stage.cpp @@ -154,7 +154,7 @@ bool Stage::is_clear() const { std::vector Stage::get_output_keys_down() const { auto keys = std::vector{ }; for (const auto& output : m_output_down) - if (is_keyboard_key(output.key) || is_mouse_button(output.key)) + if (is_device_key(output.key)) keys.push_back(output.key); return keys; } @@ -329,13 +329,17 @@ void Stage::validate_state(const std::function& is_down) { m_sequence.erase( std::remove_if(begin(m_sequence), end(m_sequence), - [&](const KeyEvent& event) { return !is_down(event.key); }), + [&](const KeyEvent& event) { + return is_device_key(event.key) && + !is_down(event.key); + }), end(m_sequence)); m_output_down.erase( std::remove_if(begin(m_output_down), end(m_output_down), [&](const OutputDown& output) { - return !is_down(get_trigger_key(output.trigger)); + return is_device_key(output.key) && + !is_down(get_trigger_key(output.trigger)); }), end(m_output_down)); } @@ -426,9 +430,7 @@ bool Stage::is_physically_pressed(Key key) const { void Stage::apply_input(const KeyEvent event, int device_index) { assert(event.state == KeyState::Down || event.state == KeyState::Up); - assert(is_keyboard_key(event.key) || - is_mouse_button(event.key) || - is_mouse_wheel(event.key) || + assert(is_device_key(event.key) || is_virtual_key(event.key) || event.key == Key::timeout);