From 60c51939439d8f703d45ba262c535eb5e2ff5298 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 4 Mar 2021 15:21:38 +0000 Subject: [PATCH] Better keypress detection --- speaker_client.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/speaker_client.py b/speaker_client.py index 0441b96b8..8a5be1a73 100644 --- a/speaker_client.py +++ b/speaker_client.py @@ -9,31 +9,29 @@ __status__ = "Production" import os import html import time +import sys +import select from session import createSession from speaker import getSpeakerFromServer from speaker import getSpeakerPitch from speaker import getSpeakerRate from speaker import getSpeakerRange -import signal def _waitForKeypress(timeout: int, debug: bool) -> str: """Waits for a keypress with a timeout Returns the key pressed, or None on timeout """ - def _nothing(sig, frame): pass - signal.signal(signal.SIGALRM, _nothing) - signal.alarm(timeout) - keyPress = None - try: - keyPress = input() - signal.alarm(0) - except (IOError, EOFError): + i, o, e = select.select([sys.stdin], [], [], timeout) + + if (i): if debug: - print('Keypress Timeout') - print('Timeout') + print("Text entered: " + sys.stdin.readline().strip()) + return sys.stdin.readline().strip() + else: + if debug: + print("Timeout") return None - return keyPress def _speakerEspeak(espeak, pitch: int, rate: int, srange: int, @@ -133,5 +131,5 @@ def runSpeakerClient(baseDir: str, proxyType: str, httpPrefix: str, prevSay = speakerJson['say'] # wait for a while, or until a key is pressed - if _waitForKeypress(10, debug): + if _waitForKeypress(30, debug): break