-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement multicharacter input
- Loading branch information
1 parent
7ed119f
commit 6e7da2c
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from sys import stdin | ||
import tty | ||
import termios | ||
from time import time as now | ||
from queue import Queue, Empty as queue_empty | ||
import threading | ||
|
||
_SHORT_TIME_SECONDS = 0.01 | ||
|
||
|
||
def _fill_queue(queue: Queue[int]): | ||
while True: | ||
queue.put(ord(stdin.read(1))) | ||
|
||
|
||
def _main() -> None: | ||
chars: list[int] = [] | ||
queue: Queue[int] = Queue() | ||
|
||
threading.Thread(target=_fill_queue, args=(queue,), daemon=True).start() | ||
|
||
char = queue.get() | ||
if char != 27: | ||
print(f"key: {char}", end="\n\r") | ||
return | ||
esc = now() | ||
chars.append(27) | ||
while now() - esc < _SHORT_TIME_SECONDS: | ||
try: | ||
char = queue.get(timeout=_SHORT_TIME_SECONDS) | ||
except queue_empty: | ||
break | ||
if char not in chars: | ||
chars.append(char) | ||
print(chars, end="\n\r") | ||
chars.clear() | ||
|
||
|
||
if __name__ == "__main__": | ||
old_settings = termios.tcgetattr(stdin) | ||
try: | ||
tty.setraw(stdin.fileno()) | ||
_main() | ||
finally: | ||
termios.tcsetattr(stdin, termios.TCSADRAIN, old_settings) | ||
print("byeeeee") |