Simplify quit: use Escape key instead of Ctrl+C

- Rollback to original tty-based input
- Add Escape key to quit
- Remove arrow key detection (use < > keys only)
- Simplify the play() method
This commit is contained in:
Michael Winter 2026-03-17 17:41:09 +01:00
parent d2c88ccc3f
commit 4a1e1f7ec2

View file

@ -76,7 +76,7 @@ class OSCSender:
print(f"\nFundamental: {self.fundamental} Hz") print(f"\nFundamental: {self.fundamental} Hz")
print(f"Destination: {self.ip}:{self.port}") print(f"Destination: {self.ip}:{self.port}")
print(f"{'=' * 50}") print(f"{'=' * 50}")
print("← Previous | Next → | Ctrl+C to quit") print("← Previous | Next → | Escape to quit")
def play(self): def play(self):
"""Interactive playback with keyboard control.""" """Interactive playback with keyboard control."""
@ -84,48 +84,35 @@ class OSCSender:
print("No chords loaded!") print("No chords loaded!")
return return
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 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: try:
import threading
import tty
import termios
import os
# Send and display first chord
self.send_chord(self.current_index)
self.display_chord(self.current_index)
def get_key():
"""Get a single keypress."""
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
print("\nUse arrow keys to navigate. Press Escape to quit.")
while True: while True:
key = get_key() key = get_key()
if key is None:
continue
# Left arrow # Escape key - quit
if key == "\x1b": # Escape sequence start if key == "\x1b":
next1 = get_key() break
if next1 == "[":
next2 = get_key()
if next2 == "D": # Left arrow
if self.current_index > 0:
self.current_index -= 1
self.send_chord(self.current_index)
self.display_chord(self.current_index)
elif next2 == "C": # Right arrow
if self.current_index < len(self.chords) - 1:
self.current_index += 1
self.send_chord(self.current_index)
self.display_chord(self.current_index)
# Alternative: use < and > keys # Alternative: use < and > keys
elif key == "," or key == "<": elif key == "," or key == "<":
@ -140,9 +127,9 @@ class OSCSender:
self.display_chord(self.current_index) self.display_chord(self.current_index)
except KeyboardInterrupt: except KeyboardInterrupt:
print("\n\nPlayback stopped.") pass # Clean exit
except Exception:
pass # Clean exit for any other error print("\n\nPlayback stopped.")
def send_all(self): def send_all(self):
"""Send all chords in sequence (for testing).""" """Send all chords in sequence (for testing)."""