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:
Michael Winter 2026-03-17 17:26:23 +01:00
parent fd4b89670e
commit d2c88ccc3f

View file

@ -84,36 +84,32 @@ class OSCSender:
print("No chords loaded!") print("No chords loaded!")
return return
import tty import select
import termios import signal
import os
# Send and display first chord # Send and display first chord
self.send_chord(self.current_index) self.send_chord(self.current_index)
self.display_chord(self.current_index) self.display_chord(self.current_index)
def get_key(): def get_key():
"""Get a single keypress. Returns None on error.""" """Get a keypress with timeout. Returns None if no key pressed."""
try: if select.select([sys.stdin], [], [], 0.1)[0]:
fd = sys.stdin.fileno() return sys.stdin.read(1)
old_settings = termios.tcgetattr(fd) return None
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
print("\nUse arrow keys to navigate. Press Ctrl+C to quit.") 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: try:
while True: while True:
key = get_key() key = get_key()
if key is None: if key is None:
break continue
# Left arrow # Left arrow
if key == "\x1b": # Escape sequence start if key == "\x1b": # Escape sequence start