File tree Expand file tree Collapse file tree 5 files changed +114
-124
lines changed
Expand file tree Collapse file tree 5 files changed +114
-124
lines changed Original file line number Diff line number Diff line change 33__version__ = "4.0.0-dev0"
44__all__ = ["readchar" , "readkey" , "key" ]
55
6+ from sys import platform
67
7- from . import key
8- from .readchar import readchar , readkey
8+
9+ if platform .startswith ("linux" ):
10+ from ._posix_read import readchar , readkey
11+ elif platform in ("win32" , "cygwin" ):
12+ from ._win_read import readchar , readkey
13+ else :
14+ raise NotImplementedError (f"The platform { platform } is not supported yet" )
Original file line number Diff line number Diff line change 1- # Initially taken from:
2- # http://code.activestate.com/recipes/134892/
3- # Thanks to Danny Yoo
41import sys
52import termios
63import tty
74
8-
5+ # Initially taken from:
6+ # http://code.activestate.com/recipes/134892/
7+ # Thanks to Danny Yoo
98def readchar ():
109 fd = sys .stdin .fileno ()
1110 old_settings = termios .tcgetattr (fd )
@@ -15,3 +14,18 @@ def readchar():
1514 finally :
1615 termios .tcsetattr (fd , termios .TCSADRAIN , old_settings )
1716 return ch
17+
18+
19+ def readkey (getchar_fn = None ):
20+ getchar = getchar_fn or readchar
21+ c1 = getchar ()
22+ if ord (c1 ) != 0x1B :
23+ return c1
24+ c2 = getchar ()
25+ if ord (c2 ) != 0x5B :
26+ return c1 + c2
27+ c3 = getchar ()
28+ if ord (c3 ) != 0x33 :
29+ return c1 + c2 + c3
30+ c4 = getchar ()
31+ return c1 + c2 + c3 + c4
Original file line number Diff line number Diff line change 1+ import msvcrt
2+ import sys
3+
4+ import key
5+
6+
7+ # Initially taken from:
8+ # http://code.activestate.com/recipes/134892/#c9
9+ # Thanks to Stephen Chappell
10+ win_encoding = "mbcs"
11+ XE0_OR_00 = "\x00 \xe0 "
12+
13+
14+ def readchar ():
15+ "Get a single character on Windows."
16+
17+ while msvcrt .kbhit ():
18+ msvcrt .getch ()
19+ ch = msvcrt .getch ()
20+ # print('ch={}, type(ch)={}'.format(ch, type(ch)))
21+ # while ch.decode(win_encoding) in unicode('\x00\xe0', win_encoding):
22+ while ch .decode (win_encoding ) in XE0_OR_00 :
23+ # print('found x00 or xe0')
24+ msvcrt .getch ()
25+ ch = msvcrt .getch ()
26+
27+ return ch if sys .version_info .major > 2 else ch .decode (encoding = win_encoding )
28+
29+
30+ # Windows uses scan codes for extended characters. This dictionary translates
31+ # scan codes to the unicode sequences expected by readkey.
32+ #
33+ # for windows scan codes see:
34+ # https://msdn.microsoft.com/en-us/library/aa299374
35+ # or
36+ # http://www.quadibloc.com/comp/scan.htm
37+ xlate_dict = {
38+ 59 : key .F1 ,
39+ 60 : key .F2 ,
40+ 61 : key .F3 ,
41+ 62 : key .F4 ,
42+ 63 : key .F5 ,
43+ 64 : key .F6 ,
44+ 65 : key .F7 ,
45+ 66 : key .F8 ,
46+ 67 : key .F9 ,
47+ 68 : key .F10 ,
48+ 133 : key .F11 ,
49+ 134 : key .F12 ,
50+ # don't have table entries for...
51+ # CTR_A, ..
52+ # ALT_A, ..
53+ # CTRL-F1, ..
54+ # CTRL_ALT_SUPR,
55+ # CTRL_ALT_A, .., etc.
56+ 82 : key .INSERT ,
57+ 83 : key .SUPR , # key.py uses SUPR, not DELETE
58+ 73 : key .PAGE_UP ,
59+ 81 : key .PAGE_DOWN ,
60+ 71 : key .HOME ,
61+ 79 : key .END ,
62+ 72 : key .UP ,
63+ 80 : key .DOWN ,
64+ 75 : key .LEFT ,
65+ 77 : key .RIGHT ,
66+ }
67+
68+
69+ def readkey (getchar_fn = None ):
70+ # Get a single character on Windows. if an extended key is pressed, the
71+ # Windows scan code is translated into a the unicode sequences readchar
72+ # expects (see key.py).
73+ while True :
74+ if msvcrt .kbhit ():
75+ ch = msvcrt .getch ()
76+ a = ord (ch )
77+ if a == 0 or a == 224 :
78+ b = ord (msvcrt .getch ())
79+ try :
80+ return xlate_dict [b ]
81+ except KeyError :
82+ return None
83+ elif a == 8 :
84+ return key .BACKSPACE
85+ elif a == 13 :
86+ return key .ENTER
87+ else :
88+ return ch .decode ()
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments