-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateKeyboard.py
executable file
·66 lines (58 loc) · 2.03 KB
/
StateKeyboard.py
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
from State import State
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
import time
import microcontroller
from KeyOptions import KeyOptions
class StateKeyboard(State):
def __init__(self, keyboard):
self.keyboard = keyboard
self.keyboard_layout = KeyboardLayoutUS(self.keyboard)
self.custom_keys = None
self.custom_password = None
try:
f = open("custom.keys", "r")
self.custom_keys_string = f.read()
self.custom_keys = eval(self.custom_keys_string)
except OSError:
self.custom_keys = None
pass
self.key_output = ""
try:
f = open("custom.password", "r")
self.custom_password = f.read()
except OSError:
self.custom_password = None
pass
def start(self):
self.pixel.fill(0x0)
nr = microcontroller.nvm[0]
if nr >= len(KeyOptions.OPTIONS):
nr = 0
self.key_output = KeyOptions.OPTIONS[nr]["keys"]
def onButtonPress(self):
self.pixel.fill((255, 0, 0))
if self.custom_password is not None:
self.make_keystrokes(self.custom_password, 0)
return
if self.custom_keys is not None:
key_output = self.custom_keys
else:
key_output = self.key_output
if isinstance(key_output, (list, tuple)) and isinstance(key_output[0], dict):
for k in key_output:
self.make_keystrokes(k['keys'], k['delay'])
else:
self.make_keystrokes(key_output, delay=0)
pass
def onButtonRelease(self):
self.pixel.fill(0x0)
def make_keystrokes(self, keys, delay):
if isinstance(keys, str):
self.keyboard_layout.write(keys)
elif isinstance(keys, int):
self.keyboard.press(keys)
self.keyboard.release_all()
elif isinstance(keys, (list, tuple)):
self.keyboard.press(*keys)
self.keyboard.release_all()
time.sleep(delay)