Keep every captured key owned until its own release
Binding capture tracked one owned key, so a second capture could overwrite an unreleased earlier choice and leak its repeats. Ownership is now a bitset over scancodes, released per key or on focus loss, with successive-capture, release order, cancel and focus-loss regressions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "render/sdl_renderer.hpp"
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <bitset>
|
||||
#include <cmath>
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_sdl3.h>
|
||||
@@ -126,9 +127,10 @@ struct Application::Impl {
|
||||
bool backlogged{false};
|
||||
bool key_capture{false}; // Binding capture armed.
|
||||
std::optional<KeyCode> captured_key; // Taken once by the host.
|
||||
// The chosen physical key stays owned by capture until its release (or focus loss):
|
||||
// its repeats, text and release reach neither the toolkit nor the world.
|
||||
std::optional<SDL_Scancode> owned_key;
|
||||
// Every chosen physical key stays owned by capture until its own release (or focus
|
||||
// loss), across successive captures: its repeats and release reach neither the
|
||||
// toolkit nor the world, and text is suppressed while any is held.
|
||||
std::bitset<SDL_SCANCODE_COUNT> owned_keys;
|
||||
// Capture sources: an external modal/launcher, the legacy Tab toggle and the
|
||||
// toolkit's own keyboard focus. World actions run only when none applies.
|
||||
bool external_capture{false}, tab_capture{false}, toolkit_capture{false};
|
||||
@@ -304,16 +306,19 @@ std::expected<void, Error> Application::poll_events() {
|
||||
: event.type == SDL_EVENT_TEXT_INPUT ? event.text.windowID == id
|
||||
: event.type == SDL_EVENT_TEXT_EDITING ? event.edit.windowID == id
|
||||
: false;
|
||||
const auto code = key_event ? static_cast<std::size_t>(event.key.scancode) : 0;
|
||||
const bool known = key_event && code < p.owned_keys.size();
|
||||
const bool owned =
|
||||
keyboard && p.owned_key && (!key_event || event.key.scancode == *p.owned_key);
|
||||
keyboard && (key_event ? known && p.owned_keys.test(code) : p.owned_keys.any());
|
||||
if (keyboard && (p.key_capture || owned)) {
|
||||
if (p.key_capture && event.type == SDL_EVENT_KEY_DOWN && !event.key.repeat) {
|
||||
p.captured_key = static_cast<KeyCode>(event.key.scancode);
|
||||
p.owned_key = event.key.scancode;
|
||||
if (known)
|
||||
p.owned_keys.set(code);
|
||||
p.key_capture = false;
|
||||
p.update_capture(); // Drains the rest of this batch before world input.
|
||||
} else if (owned && event.type == SDL_EVENT_KEY_UP) {
|
||||
p.owned_key.reset();
|
||||
p.owned_keys.reset(code); // Only that key; others stay owned.
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -355,8 +360,8 @@ std::expected<void, Error> Application::poll_events() {
|
||||
if (!p.state.accepts_input()) {
|
||||
p.press.reset(); // A release may never arrive for this window.
|
||||
p.ui_press = false;
|
||||
p.owned_key.reset(); // The toolkit clears its own keys on focus loss.
|
||||
if (p.key_capture) { // Focus loss cancels binding capture without a key.
|
||||
p.owned_keys.reset(); // The toolkit clears its own keys on focus loss.
|
||||
if (p.key_capture) { // Focus loss cancels binding capture without a key.
|
||||
p.key_capture = false;
|
||||
p.update_capture();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user