Use select with timeout for keyboard input
- Replaces tty-based input with select-based approach - Adds signal handler for Ctrl+C - Allows KeyboardInterrupt to propagate correctly
This commit is contained in:
parent
fd4b89670e
commit
d2c88ccc3f
|
|
@ -84,36 +84,32 @@ class OSCSender:
|
|||
print("No chords loaded!")
|
||||
return
|
||||
|
||||
import tty
|
||||
import termios
|
||||
import os
|
||||
import select
|
||||
import signal
|
||||
|
||||
# Send and display first chord
|
||||
self.send_chord(self.current_index)
|
||||
self.display_chord(self.current_index)
|
||||
|
||||
def get_key():
|
||||
"""Get a single keypress. Returns None on error."""
|
||||
try:
|
||||
fd = sys.stdin.fileno()
|
||||
old_settings = termios.tcgetattr(fd)
|
||||
try:
|
||||
tty.setraw(sys.stdin.fileno())
|
||||
ch = sys.stdin.read(1)
|
||||
return ch
|
||||
finally:
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
||||
except (termios.error, OSError):
|
||||
# Fallback for non-tty environments
|
||||
return sys.stdin.read(1) if sys.stdin else None
|
||||
"""Get a keypress with timeout. Returns None if no key pressed."""
|
||||
if select.select([sys.stdin], [], [], 0.1)[0]:
|
||||
return sys.stdin.read(1)
|
||||
return None
|
||||
|
||||
print("\nUse arrow keys to navigate. Press Ctrl+C to quit.")
|
||||
|
||||
# Handle SIGINT explicitly
|
||||
def handle_sigint(signum, frame):
|
||||
raise KeyboardInterrupt
|
||||
|
||||
signal.signal(signal.SIGINT, handle_sigint)
|
||||
|
||||
try:
|
||||
while True:
|
||||
key = get_key()
|
||||
if key is None:
|
||||
break
|
||||
continue
|
||||
|
||||
# Left arrow
|
||||
if key == "\x1b": # Escape sequence start
|
||||
|
|
|
|||
Loading…
Reference in a new issue