Skip to content

Fix stale keysym entries when modifier changes while key is held#4747

Merged
tarek-y-ismail merged 1 commit intoadd-keyboard-state-trackerfrom
copilot/sub-pr-4746
Mar 9, 2026
Merged

Fix stale keysym entries when modifier changes while key is held#4747
tarek-y-ismail merged 1 commit intoadd-keyboard-state-trackerfrom
copilot/sub-pr-4746

Conversation

Copy link
Contributor

Copilot AI commented Mar 5, 2026

KeyboardStateTracker used the keysym from the key-up event to erase from pressed_keysyms, but that keysym can differ from what was stored at key-down when a modifier changes mid-hold (e.g. 1 down → Shift down → key-up reports XKB_KEY_exclam, leaving XKB_KEY_1 stranded).

Related: #4746

What's new?

  • scancode_to_keysym map replaces the two separate sets (pressed_keysyms / pressed_scancodes). Scancode is the stable identity of a physical key; the map stores the keysym recorded at key-down against it.
  • Key-up erases by scancode (scancode_to_keysym.erase(scancode)), so mismatched key-up keysyms caused by held modifiers never leave stale entries.
  • Shift transitions mutate map values in-place via a range loop applying xkb_keysym_to_upper/xkb_keysym_to_lower, replacing the previous set-rebuild approach.
  • keysym_is_pressed uses std::ranges::any_of over map values; scancode_is_pressed uses map.contains. The linear scan for keysyms is inconsequential given the physical bound on simultaneously held keys (≤ ~20).
  • New test key_up_clears_key_when_modifier_changed_while_held exercises the exact failure scenario: 1 down → Shift down → ! up → neither XKB_KEY_1 nor XKB_KEY_exclam remains pressed.

How to test

Run the KeyboardStateTrackerTest suite:

./bin/mir_unit_tests.bin --gtest_filter="KeyboardStateTrackerTest.*"

All 17 tests should pass, including the new regression test.

Checklist

  • Tests added and pass
  • Adequate documentation added
  • (optional) Added Screenshots or videos

Copy link
Contributor Author

Copilot AI commented Mar 5, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Copilot AI changed the title [WIP] Update keyboard state tracker implementation based on review feedback Fix stale keysym entries when modifier changes while key is held Mar 5, 2026
Copy link
Contributor

@tarek-y-ismail tarek-y-ismail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. @AlanGriffiths What do you think?

@tarek-y-ismail tarek-y-ismail force-pushed the add-keyboard-state-tracker branch from 3d1ec08 to fa7ae7d Compare March 5, 2026 14:44
@tarek-y-ismail tarek-y-ismail marked this pull request as ready for review March 5, 2026 15:28
@tarek-y-ismail tarek-y-ismail requested a review from a team as a code owner March 5, 2026 15:28
@tarek-y-ismail tarek-y-ismail force-pushed the add-keyboard-state-tracker branch 2 times, most recently from ba462f0 to 13bf364 Compare March 6, 2026 13:10
@tarek-y-ismail
Copy link
Contributor

@copilot Rebase this branch on main and fix any conflicts.

@tarek-y-ismail
Copy link
Contributor

Fine, I'll do it myself 🤦

Later...

@tarek-y-ismail tarek-y-ismail merged commit 16bc38f into add-keyboard-state-tracker Mar 9, 2026
29 of 32 checks passed
@tarek-y-ismail tarek-y-ismail deleted the copilot/sub-pr-4746 branch March 9, 2026 09:45
@github-actions
Copy link

github-actions bot commented Mar 9, 2026

TICS Quality Gate

✔️ Passed

mir

Coding Standards: ✔️ Passed

✔️ Condition “No new Coding Standard Violations for level 1, 2, 3 with respect to Previous analysis” passed.

See the results in the TICS Viewer

The following files have been checked for this project
  • src/server/frontend_wayland/keyboard_state_tracker.cpp
  • src/server/frontend_wayland/keyboard_state_tracker.h

TICS / TICS / Run TICS analysis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants