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:
dingo
2026-10-02 16:15:54 -04:00
co-authored by Claude Opus 5.5
parent 14e676426f
commit 582d4baa20
5 changed files with 110 additions and 10 deletions
+70
View File
@@ -667,6 +667,76 @@ void capture_balances_toolkit() {
// World input still works with fresh presses.
push(SDL_SCANCODE_W, true);
CHECK(app->poll_events() && move_y() == 1000);
push(SDL_SCANCODE_W, false);
CHECK(app->poll_events() && move_y() == 0);
// Successive captures (follow-up review): every chosen key stays owned until its
// own release; a second capture never overwrites the first.
const auto choose = [&](SDL_Scancode scancode) {
app->begin_key_capture();
push(scancode, true);
CHECK(app->poll_events() && app->take_captured_key() == static_cast<KeyCode>(scancode));
};
const auto quiet = [&](SDL_Scancode repeated) { // Repeat + text: nothing reaches anyone.
push(repeated, true, true);
text("x");
CHECK(app->poll_events() && queued_keyboard_events() == 0 && move_y() == 0);
CHECK(app->present(frame) == Step::rendered);
};
const auto fresh = [&](SDL_Scancode scancode, ImGuiKey toolkit_key) { // Positive control.
push(scancode, true);
CHECK(app->poll_events() && queued_keyboard_events() > 0);
CHECK(app->present(frame) == Step::rendered && ImGui::IsKeyDown(toolkit_key));
push(scancode, false);
frame_once();
CHECK(!ImGui::IsKeyDown(toolkit_key));
};
// a) W (a world key) then K chosen while W is held; K released first.
app->set_ui_capture(true);
choose(SDL_SCANCODE_W);
choose(SDL_SCANCODE_K);
app->set_ui_capture(false);
push(SDL_SCANCODE_K, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
quiet(SDL_SCANCODE_W);
CHECK(!ImGui::IsKeyDown(ImGuiKey_W) && !ImGui::IsKeyDown(ImGuiKey_K));
push(SDL_SCANCODE_W, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
push(SDL_SCANCODE_W, true); // Fresh press: world and toolkit both see it.
CHECK(app->poll_events() && move_y() == 1000);
CHECK(app->present(frame) == Step::rendered && ImGui::IsKeyDown(ImGuiKey_W));
push(SDL_SCANCODE_W, false);
frame_once();
fresh(SDL_SCANCODE_K, ImGuiKey_K);
// b) Reverse release order: J then K chosen; J released first, K still owned.
choose(SDL_SCANCODE_J);
choose(SDL_SCANCODE_K);
push(SDL_SCANCODE_J, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
quiet(SDL_SCANCODE_K);
push(SDL_SCANCODE_K, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
text("x"); // Nothing owned any more: text flows again.
CHECK(app->poll_events() && queued_keyboard_events() > 0);
frame_once();
fresh(SDL_SCANCODE_J, ImGuiKey_J);
fresh(SDL_SCANCODE_K, ImGuiKey_K);
// c) Cancel during the second capture keeps the first key owned.
choose(SDL_SCANCODE_J);
app->begin_key_capture();
app->cancel_key_capture();
quiet(SDL_SCANCODE_J);
push(SDL_SCANCODE_J, false);
CHECK(app->poll_events() && queued_keyboard_events() == 0);
fresh(SDL_SCANCODE_J, ImGuiKey_J);
// d) Focus loss frees every owned key.
choose(SDL_SCANCODE_J);
choose(SDL_SCANCODE_K);
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
CHECK(app->poll_events() && app->poll_events());
frame_once();
fresh(SDL_SCANCODE_J, ImGuiKey_J);
fresh(SDL_SCANCODE_K, ImGuiKey_K);
// While paused (hidden) nothing is drawn, yet the toolkit queue stays bounded.
send(SDL_EVENT_WINDOW_HIDDEN, id);
for (int round = 0; round < 20; ++round) {