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:
parent
d2c88ccc3f
commit
4a1e1f7ec2
|
|
@ -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
|
try:
|
||||||
import signal
|
import threading
|
||||||
|
import tty
|
||||||
|
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 keypress with timeout. Returns None if no key pressed."""
|
"""Get a single keypress."""
|
||||||
if select.select([sys.stdin], [], [], 0.1)[0]:
|
fd = sys.stdin.fileno()
|
||||||
return sys.stdin.read(1)
|
old_settings = termios.tcgetattr(fd)
|
||||||
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:
|
||||||
|
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:
|
||||||
|
pass # Clean exit
|
||||||
|
|
||||||
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)."""
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue