Skip to content

Commit 11c6965

Browse files
committed
add alt-codes helper
1 parent 38a685a commit 11c6965

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

libraries/helpers/alt_codes.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# SPDX-FileCopyrightText: 2021 Neradoc [email protected]
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Courtesy of https://github.com/nico7885
6+
"""
7+
8+
from adafruit_hid.keycode import Keycode
9+
10+
class Altcode(Keycode):
11+
unicodeChar = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"
12+
asciiexValue = (128, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 142, 145, 146, 147, 148, 149, 150,
13+
151, 152, 153, 154, 155, 156, 158, 159)
14+
15+
def __init__(self, keyboard, layout):
16+
self.keyboard = keyboard
17+
self.layout = layout
18+
19+
def _special_char(self, char_value):
20+
if char_value == 0x0A or char_value == 0x0D:
21+
self.keyboard.send(Keycode.ENTER)
22+
elif char_value == 0x09:
23+
self.keyboard.send(Keycode.TAB)
24+
else:
25+
raise ValueError("Value of char is unknown:", char_value)
26+
27+
28+
def _get_value_of_char(self, char):
29+
# Si le caractère fait partie de la table ASCII étendue
30+
# renvoie la valeur ASCII étendue du caractère
31+
# sinon renvoie la valeur Unicode
32+
indice = self.unicodeChar.find(char)
33+
if indice != -1:
34+
char_val = self.asciiexValue[indice]
35+
else:
36+
char_val = ord(char)
37+
return char_val
38+
39+
40+
def _num_to_keypad(self, string):
41+
for char in string:
42+
if char.isdigit():
43+
num = int(char)
44+
if num > 0:
45+
# Ne pas utiliser la fonction send qui effectue un releaseAll
46+
self.keyboard.press(self.KEYPAD_ONE + num - 1)
47+
self.keyboard.release(self.KEYPAD_ONE + num - 1)
48+
elif num == 0:
49+
self.keyboard.press(self.KEYPAD_ZERO)
50+
self.keyboard.release(self.KEYPAD_ZERO)
51+
else:
52+
raise ValueError("Char is not a digit:", char)
53+
break
54+
55+
56+
def _win_alt_code_CP1252(self, asciiex_string):
57+
len_asciiex = len(asciiex_string)
58+
self.keyboard.press(self.ALT)
59+
for x in range(4-len_asciiex):
60+
# Ne pas utiliser la fonction send qui effectue un releaseAll
61+
self.keyboard.press(self.KEYPAD_ZERO)
62+
self.keyboard.release(self.KEYPAD_ZERO)
63+
self._num_to_keypad(asciiex_string)
64+
self.keyboard.release_all()
65+
66+
67+
def _win_alt_unicode_point(self, unicode_string):
68+
self.keyboard.press(self.ALT)
69+
self._num_to_keypad(unicode_string)
70+
self.keyboard.release_all()
71+
72+
73+
def windows(self, string):
74+
for char in string:
75+
char_val = self._get_value_of_char(char)
76+
if char_val < 32:
77+
# pour les valeurs TAB et \n
78+
self._special_char(char_val)
79+
elif char_val < 256:
80+
self._win_alt_code_CP1252(str(char_val))
81+
else:
82+
self._win_alt_unicode_point(str(char_val))
83+
84+
85+
def _linux_alt_unicode_point(self, unicode_stringHex):
86+
self.keyboard.send(self.CONTROL, self.SHIFT, self.U)
87+
for char in unicode_stringHex[2:]:
88+
# on n'envoie pas le shift ou le altgr ici!
89+
# seul la position de la touche est nécessaire!
90+
self.keyboard.send(self.layout.keycodes(char)[-1])
91+
self.keyboard.send(self.ENTER)
92+
93+
94+
def linux(self, string):
95+
for char in string:
96+
char_val = ord(char)
97+
if char_val < 32:
98+
# pour les valeurs TAB et \n
99+
self._special_char(char_val)
100+
else:
101+
self._linux_alt_unicode_point(hex(char_val))

0 commit comments

Comments
 (0)