-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi
More file actions
38 lines (30 loc) · 888 Bytes
/
i
File metadata and controls
38 lines (30 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
nput_handler.pyimport logging
import threading
from typing import Callable
log = logging.getLogger(__name__)
class HotkeyManager:
"""Keyboard hotkey handler using pynput."""
def __init__(self):
self._bindings: dict = {}
self._listener = None
def register(self, key_name: str, callback: Callable):
self._bindings[key_name.lower()] = callback
def listen(self):
try:
from pynput import keyboard
def on_press(key):
try:
name = key.name if hasattr(key, "name") else str(key.char)
if name.lower() in self._bindings:
self._bindings[name.lower()]()
except Exception as e:
log.debug("Hotkey error: %s", e)
self._listener = keyboard.Listener(on_press=on_press)
self._listener.start()
self._listener.join()
except ImportError:
log.warning("pynput not available — hotkeys disabled")
threading.Event().wait()
def stop(self):
if self._listener:
self._listener.stop()