-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypress.py
More file actions
199 lines (132 loc) · 3.56 KB
/
keypress.py
File metadata and controls
199 lines (132 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Python 3.6.2
# ----------------------------------------------------------------------------
"""Capture de touche clavier pressée.
Utilisation:
```
import keypress
if keypress.getName() == 'Tab':
print('Tab !')
if keypress.getKey() == '\t':
print('Tab !')
```
"""
__title__ = "keypress"
__author__ = "Asurix"
__license__ = "MIT"
__version__ = "1.0.0"
__github__ = "https://github.com/4surix/keypress"
import string
import platform
name_system = platform.system()
if name_system == 'Linux':
import sys, tty, termios
def getKey() -> str:
key = ''
descripteur_fichier:int = sys.stdin.fileno()
anciens_parametres = termios.tcgetattr(descripteur_fichier)
try:
# Définit le mode du descripteur de fichier à row.
tty.setraw(descripteur_fichier)
# Lis le prochain caractère.
key = sys.stdin.read(1)
finally:
# Remet les valeurs du descripteur de fichier comme avant
# après la transmission de toute sortie en file d’attente.
termios.tcsetattr(
descripteur_fichier, termios.TCSADRAIN, anciens_parametres
)
return key
elif name_system == 'Windows':
import time
from msvcrt import getch, kbhit
def getKey() -> str:
while not kbhit():
time.sleep(0.1)
return chr(getch()[0])
elif name_system == 'Darwin':
import Carbon
EventAvail = Carbon.Evt.EventAvail
GetNextEvent = Carbon.Evt.GetNextEvent
def getKey() -> str:
if EventAvail(0x0008)[0] == 0:
return ''
else:
return chr(GetNextEvent(0x0008)[1][1] & 0x000000FF)
COMBI_CTRL = {
chr(i): 'Ctrl+' + carac
for i, carac in enumerate(string.ascii_uppercase, start=1)
}
COMBI_ALTGR = {
chr(i): 'AltGr+' + carac
for i, carac in enumerate(
'AZERTYUIOP????QSDFGHJKLM????WXCVBN',
start=16
)
}
def getName() -> str:
key = getKey()
if key == '\x08':
key = 'BackSpace'
elif key == '\x1b':
key = 'Escape'
elif key == '\r':
key = 'Return'
elif key == '\t':
key = 'Tab'
elif key == '\xe0':
key = getKey()
if key == 'H':
key = 'Up'
elif key == 'P':
key = 'Down'
elif key == 'K':
key = 'Left'
elif key == 'M':
key = 'Right'
elif key == 'S':
key = 'Delete'
elif key == 'R':
key = 'Insert'
elif key == 'Q':
key = 'PageUp'
elif key == 'I':
key = 'PageDown'
elif key == 'G':
key = 'Begin'
elif key == 'O':
key = 'End'
elif key == '\x85':
key = 'F11'
elif key == '\x86':
key = 'F12'
elif key == '\x00':
key = getKey()
if key == ';':
key = 'F1'
elif key == '<':
key = 'F2'
elif key == '=':
key = 'F3'
elif key == '>':
key = 'F4'
elif key == '?':
key = 'F5'
elif key == '@':
key = 'F6'
elif key == 'A':
key = 'F7'
elif key == 'B':
key = 'F8'
elif key == 'C':
key = 'F9'
elif key == 'D':
key = 'F10'
elif key == '£':
key = 'AltGr+Delete'
else:
key = COMBI_ALTGR.get(key, key)
else:
key = COMBI_CTRL.get(key, key)
return key