Skip to content

fix: a Command shortcut no longer leave Command held down - #71

Open
bluzername wants to merge 1 commit into
awlevin:mainfrom
bluzername:fix-command-key-stuck-down
Open

bluzername wants to merge 1 commit into
awlevin:mainfrom
bluzername:fix-command-key-stuck-down

Conversation

@bluzername

Copy link
Copy Markdown

Fixes part of #58 (bug 7, "A Command shortcut leaves Command held down").

press(key, command=True) set the Command flag on the key's own events but never send anything for the Command key itself. On macOS 27 the system then keep Command down after the press (CGEventSourceFlagsState read it held until something send a Command key-up). After that, every letter from type_text arrive as Command+key so nothing reach the document, and the next click is a Command-click. It follow go_back (Command-[) and clear_field (Command-A) too.

Fix: send Command's own key down before the key and up after it, in a finally so an interrupted press still release it.

-    _down_then_up(event)
+    if not command:
+        _down_then_up(event)
+        return
+    try:
+        _post(_command_key(True))
+        _down_then_up(event)
+    finally:
+        _post(_command_key(False))

Test (TDD): added two tests in tests/test_input_abort.py:

  • test_a_command_shortcut_sends_the_command_key_down_before_and_up_after_the_key checks the full down/up sequence around a Command shortcut.
  • test_the_command_key_goes_back_up_even_when_posting_it_is_interrupted checks the Command key still goes back up when posting it get interrupted.

Both fail on main (only 2 events post, the key's own, with the flag set but no separate Command key events) and pass with the fix. Renamed the old test_a_key_goes_back_up_when_interrupted_after_key_down to test_a_plain_key_goes_back_up_when_interrupted_after_key_down and dropped command=True from it, since it was really testing the plain key path, and its old assertion (one event pair with the flag set, no separate Command events) is not what command=True produce anymore with this fix.

Verification: uv run ruff check . && uv run ruff format --check . clean, uv run pytest -q: 732 passed, 2 skipped, 1 xfailed (main has 730, net +2 tests here).

🤖 Generated with Claude Code

Fixes part of awlevin#58 (bug 7, "A Command shortcut leaves Command held down").

press(key, command=True) set the Command flag on the key's own events but
never send anything for the Command key itself. On macOS 27 the system then
keep Command down after the press (CGEventSourceFlagsState read it held
until something send a Command key-up). After that, every letter from
type_text arrive as Command+key so nothing reach the document, and the
next click is a Command-click. It follow go_back (Command-[) and
clear_field (Command-A) too.

Fix: send Command's own key down before the key and up after it, in a
finally so an interrupted press still release it.

Test (TDD): added two tests in tests/test_input_abort.py.
test_a_command_shortcut_sends_the_command_key_down_before_and_up_after_the_key
checks the full down/up sequence around a Command shortcut.
test_the_command_key_goes_back_up_even_when_posting_it_is_interrupted checks
the Command key still goes back up when posting it get interrupted. Both
fail on main (only 2 events post, the key's own, with the flag set but no
separate Command key events) and pass with the fix. Renamed the old
test_a_key_goes_back_up_when_interrupted_after_key_down to
test_a_plain_key_goes_back_up_when_interrupted_after_key_down and dropped
command=True from it, since it is testing the plain key path, not this one,
and its old assertion (one event pair with the flag set) is not what
command=True produce anymore.

Verification: uv run ruff check . && uv run ruff format --check . clean,
uv run pytest -q: 732 passed, 2 skipped, 1 xfailed (main has 730, net +2
tests here).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

1 participant