Fix Ctrl+C handling in OSC playback

This commit is contained in:
Michael Winter 2026-03-17 17:19:52 +01:00
parent ea3acf9efe
commit fd4b89670e

View file

@ -84,31 +84,36 @@ class OSCSender:
print("No chords loaded!") print("No chords loaded!")
return return
try: import tty
import threading import termios
import tty import os
import termios
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.""" """Get a single keypress. Returns None on error."""
try:
fd = sys.stdin.fileno() fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd) old_settings = termios.tcgetattr(fd)
try: try:
tty.setraw(sys.stdin.fileno()) tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1) ch = sys.stdin.read(1)
return ch
finally: finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch 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.")
try:
while True: while True:
key = get_key() key = get_key()
if key is None:
break
# Left arrow # Left arrow
if key == "\x1b": # Escape sequence start if key == "\x1b": # Escape sequence start
@ -140,6 +145,8 @@ class OSCSender:
except KeyboardInterrupt: except KeyboardInterrupt:
print("\n\nPlayback stopped.") print("\n\nPlayback stopped.")
except Exception:
pass # Clean exit for any other error
def send_all(self): def send_all(self):
"""Send all chords in sequence (for testing).""" """Send all chords in sequence (for testing)."""