Add method to get up/down/enter keystrokes.
authorScott Gasch <[email protected]>
Tue, 24 May 2022 23:13:32 +0000 (16:13 -0700)
committerScott Gasch <[email protected]>
Tue, 24 May 2022 23:13:32 +0000 (16:13 -0700)
input_utils.py

index 5e36db1ba853884636a2df7a5007004aa086760b..d958db23691f910acd84b9f2ee33473ff4bee96a 100644 (file)
@@ -85,6 +85,23 @@ def press_any_key(
     return single_keystroke_response(None, prompt=prompt, timeout_seconds=timeout_seconds)
 
 
+def up_down_enter() -> Optional[str]:
+    os_special_keystrokes = [3, 26]  # ^C, ^Z
+    while True:
+        key = readchar.readkey()
+        if len(key) == 1:
+            if ord(key) in os_special_keystrokes:
+                return None
+            if ord(key) == 13:
+                return 'enter'
+        elif len(key) == 3:
+            if ord(key[0]) == 27 and ord(key[1]) == 91:
+                if ord(key[2]) == 65:
+                    return "up"
+                elif ord(key[2]) == 66:
+                    return "down"
+
+
 def keystroke_helper() -> None:
     """Misc util to watch keystrokes and report what they were."""