Skip to content

Commit f7bf855

Browse files
Update readchar_linux.py
gives characters a name versus codes
1 parent 6f6591a commit f7bf855

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

readchar/readchar_linux.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# -*- coding: utf-8 -*-
2-
# Initially taken from:
3-
# http://code.activestate.com/recipes/134892/
4-
# Thanks to Danny Yoo
2+
# Copyright (c) 2014, 2015 Miguel Ángel García (@magmax9).
3+
# Based on previous work on gist getch()-like unbuffered character
4+
# reading from stdin on both Windows and Unix (Python recipe),
5+
# started by Danny Yoo. Licensed under the MIT license.
6+
57
import sys
68
import os
79
import select
@@ -13,22 +15,40 @@
1315
def readchar(wait_for_char=True):
1416
old_settings = termios.tcgetattr(sys.stdin)
1517
tty.setcbreak(sys.stdin.fileno())
16-
char_buffer = ''
18+
charbuffer = ''
19+
20+
while True:
21+
if charbuffer in key.ESCAPE_SEQUENCES:
22+
char1 = getkey(False, old_settings)
23+
else:
24+
char1 = getkey(wait_for_char, old_settings)
25+
if (charbuffer + char1) not in key.ESCAPE_SEQUENCES:
26+
# escape sequence complete or not an escape character..
27+
return convertchar(charbuffer + char1)
28+
29+
# handle cases where the escape is finished, but looks incomplete -
30+
# such as a plain old 'ESC' (\x1b) that is not followed by other
31+
# codes:
32+
if (charbuffer + char1) == charbuffer:
33+
return convertchar(charbuffer)
34+
35+
charbuffer += char1
36+
37+
38+
def getkey(wait_for_char, old_settings):
39+
charbuffer = ''
1740
try:
1841
if wait_for_char or select.select([sys.stdin, ], [], [], 0.0)[0]:
1942
char = os.read(sys.stdin.fileno(), 1)
20-
char_buffer = char if type(char) is str else char.decode()
43+
charbuffer = char if type(char) is str else char.decode()
2144
except Exception:
22-
char_buffer = ''
45+
pass
2346
finally:
2447
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
48+
return charbuffer
2549

26-
while True:
27-
if char_buffer not in key.ESCAPE_SEQUENCES:
28-
return char_buffer
29-
else:
30-
c = readchar(False)
31-
if c is None:
32-
return char_buffer
33-
else:
34-
char_buffer += c
50+
51+
def convertchar(charbuffer):
52+
if charbuffer in key.linux_keys:
53+
return key.linux_keys[charbuffer]
54+
return charbuffer

0 commit comments

Comments
 (0)