1
+ # Part of the ROBOID project - https://roboticsware.uz
2
+ # Copyright (C) 2022 RoboticsWare ([email protected] )
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General
15
+ # Public License along with this library; if not, write to the
16
+ # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17
+ # Boston, MA 02111-1307 USA
18
+
19
+ import os
20
+
21
+
22
+ if os .name == "nt" : # sys.platform == "win32":
23
+ import msvcrt
24
+
25
+ class Keyboard (object ):
26
+ BACKSPACE = 8
27
+ TAB = 9
28
+ ENTER = 13
29
+ ESC = 27
30
+ F1 = 59
31
+ F2 = 60
32
+ F3 = 61
33
+ F4 = 62
34
+ F5 = 63
35
+ F6 = 64
36
+ F7 = 65
37
+ F8 = 66
38
+ F9 = 67
39
+ F10 = 68
40
+ F11 = 133
41
+ F12 = 134
42
+
43
+ HOME = 71
44
+ UP = 72
45
+ PAGE_UP = 73
46
+ LEFT = 75
47
+ RIGHT = 77
48
+ END = 79
49
+ DOWN = 80
50
+ PAGE_DOWN = 81
51
+ INSERT = 82
52
+ DELETE = 83
53
+
54
+ _special_keys = (BACKSPACE , TAB , ENTER , ESC )
55
+ _function_numpads = (F1 , F2 , F3 , F4 , F5 , F6 , F7 , F8 , F9 , F10 , HOME , UP , PAGE_UP , LEFT , RIGHT , END , DOWN , PAGE_DOWN , INSERT , DELETE )
56
+
57
+ @staticmethod
58
+ def read ():
59
+ if msvcrt .kbhit ():
60
+ key = msvcrt .getch ()
61
+ code = ord (key )
62
+ if code == 224 : # special key (F11, F12, HOME, UP, PAGE_UP, LEFT, RIGHT, END, DOWN, PAGE_DOWN, INSERT, DELETE)
63
+ return ord (msvcrt .getch ())
64
+ elif code == 0 : # function key (F1 - F10) or num pad
65
+ code = ord (msvcrt .getch ())
66
+ if code in Keyboard ._function_numpads :
67
+ return code
68
+ elif code in Keyboard ._special_keys :
69
+ return code
70
+ else :
71
+ return key .decode ("utf-8" )
72
+ else :
73
+ import sys
74
+ import termios
75
+ from contextlib import contextmanager
76
+
77
+ class Keyboard (object ):
78
+ BACKSPACE = 127
79
+ TAB = 9
80
+ ENTER = 10
81
+ F1 = 80 # 27 79 80
82
+ F2 = 81 # 27 79 81
83
+ F3 = 82 # 27 79 82
84
+ F4 = 83 # 27 79 83
85
+ F5 = 53 # 27 91 49 53 126
86
+ F6 = 55 # 27 91 49 55 126
87
+ F7 = 56 # 27 91 49 56 126
88
+ F8 = 57 # 27 91 49 57 126
89
+ F9 = 48 # 27 91 50 48 126
90
+ F10 = 49 # 27 91 50 49 126
91
+ F11 = 51
92
+ F12 = 52 # 27 91 50 52 126
93
+
94
+ HOME = 72 # 27 91 72
95
+ UP = 65 # 27 91 65
96
+ PAGE_UP = 153 # 27 91 53 126
97
+ LEFT = 68 # 27 91 68
98
+ RIGHT = 67 # 27 91 67
99
+ END = 70 # 27 91 70
100
+ DOWN = 66 # 27 91 66
101
+ PAGE_DOWN = 154 # 27 91 54 126
102
+ INSERT = 150 # 27 91 50 126
103
+ DELETE = 151 # 27 91 51 126
104
+
105
+ _special_keys = (BACKSPACE , TAB , ENTER )
106
+
107
+ @staticmethod
108
+ @contextmanager
109
+ def _mode (file ):
110
+ old_attrs = termios .tcgetattr (file .fileno ())
111
+ new_attrs = old_attrs [:]
112
+ new_attrs [3 ] = new_attrs [3 ] & ~ (termios .ECHO | termios .ICANON )
113
+ try :
114
+ termios .tcsetattr (file .fileno (), termios .TCSADRAIN , new_attrs )
115
+ yield
116
+ finally :
117
+ termios .tcsetattr (file .fileno (), termios .TCSADRAIN , old_attrs )
118
+
119
+ @staticmethod
120
+ def read ():
121
+ with Keyboard ._mode (sys .stdin ):
122
+ key = sys .stdin .read (1 )
123
+ code = ord (key )
124
+ if code == 27 : # special key
125
+ code = ord (sys .stdin .read (1 ))
126
+ if code == 79 :
127
+ return ord (sys .stdin .read (1 ))
128
+ elif code == 91 :
129
+ code = ord (sys .stdin .read (1 ))
130
+ if code >= 65 :
131
+ return code
132
+ else :
133
+ code2 = ord (sys .stdin .read (1 ))
134
+ if code2 == 126 :
135
+ return code + 100
136
+ else :
137
+ sys .stdin .read (1 ) # 126
138
+ return code2
139
+ else :
140
+ return code
141
+ elif code in Keyboard ._special_keys :
142
+ return code
143
+ else :
144
+ return key
0 commit comments