fix: suppress key-repeat events in pynput hotkey listener
Holding the hotkey caused dozens of on_press callbacks due to OS key-repeat. Added a _pressed guard flag so only the first press and actual release fire. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -89,3 +89,26 @@ class TestPynputHotkeyListener:
|
||||
listener = PynputHotkeyListener("KEY_F12")
|
||||
await listener._handle_key_event(key_down=True)
|
||||
await listener._handle_key_event(key_down=False)
|
||||
|
||||
def test_key_repeat_ignored(self):
|
||||
from whisper_local.hotkey._pynput import PynputHotkeyListener
|
||||
from pynput.keyboard import Key
|
||||
listener = PynputHotkeyListener("KEY_F12")
|
||||
listener._loop = MagicMock()
|
||||
|
||||
# Erstes Press — wird durchgelassen
|
||||
listener._on_press(Key.f12)
|
||||
assert listener._loop.call_soon_threadsafe.call_count == 1
|
||||
|
||||
# Wiederholte Presses (Key-Repeat) — werden ignoriert
|
||||
listener._on_press(Key.f12)
|
||||
listener._on_press(Key.f12)
|
||||
assert listener._loop.call_soon_threadsafe.call_count == 1
|
||||
|
||||
# Release — wird durchgelassen
|
||||
listener._on_release(Key.f12)
|
||||
assert listener._loop.call_soon_threadsafe.call_count == 2
|
||||
|
||||
# Erneutes Release ohne Press — wird ignoriert
|
||||
listener._on_release(Key.f12)
|
||||
assert listener._loop.call_soon_threadsafe.call_count == 2
|
||||
|
||||
Reference in New Issue
Block a user