Skip to content

Commit 936b1f3

Browse files
authored
Upload code.py
1 parent 1368d01 commit 936b1f3

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

code.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import time
2+
import usb_hid
3+
from adafruit_hid.keyboard import Keyboard
4+
from adafruit_hid.keycode import Keycode
5+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
6+
from keyboard import char_to_keycode
7+
8+
debug = True
9+
10+
if debug: print("Starting...")
11+
12+
kbd = Keyboard(usb_hid.devices)
13+
layout = KeyboardLayoutUS(kbd)
14+
15+
with open("payload.txt") as file:
16+
lines = file.read().splitlines()
17+
18+
if debug: print("Printing lines of payload", lines)
19+
20+
default_delay = 0
21+
previous_line = ""
22+
def process_line(line):
23+
if debug: print("LINE:", line)
24+
25+
args = line.split(" ", 1)
26+
if debug: print("ARGS:", args)
27+
28+
inst = args[0]
29+
if debug: print("INST:", inst)
30+
31+
if inst == "REM":
32+
return;
33+
34+
global default_delay
35+
if inst == "DEFAULT_DELAY" or inst == "DEFAULTDELAY":
36+
try:
37+
default_delay = int(args[1])
38+
except ValueError:
39+
print("Invalid integer")
40+
return
41+
42+
if default_delay > 0:
43+
time.sleep(default_delay / 1000)
44+
45+
if inst == "DELAY":
46+
try:
47+
time.sleep(int(args[1]) / 1000)
48+
except ValueError:
49+
print("Invalid integer")
50+
return
51+
52+
if inst == "STRING":
53+
layout.write(args[1])
54+
55+
if inst == "GUI" or inst == "WINDOWS":
56+
kbd.send(Keycode.WINDOWS, char_to_keycode(args[1]))
57+
58+
if inst == "APP" or inst == "MENU":
59+
kbd.send(Keycode.APPLICATION)
60+
61+
if inst == "SHIFT":
62+
kbd.send(Keycode.SHIFT, char_to_keycode(args[1]))
63+
64+
if inst == "ALT":
65+
kbd.send(Keycode.ALT, char_to_keycode(args[1]))
66+
67+
if inst == "CONTROL" or inst == "CTRL":
68+
kbd.send(Keycode.CONTROL, char_to_keycode(args[1]))
69+
70+
if inst == "DOWNARROW" or inst == "DOWN":
71+
kbd.send(Keycode.DOWN_ARROW)
72+
73+
if inst == "LEFTARROW" or inst == "LEFT":
74+
kbd.send(Keycode.LEFT_ARROW)
75+
76+
if inst == "RIGHTARROW" or inst == "RIGHT":
77+
kbd.send(Keycode.RIGHT_ARROW)
78+
79+
if inst == "UPARROW" or inst == "UP":
80+
kbd.send(Keycode.UP_ARROW)
81+
82+
if inst == "BREAK" or inst == "PAUSE":
83+
kbd.send(Keycode.PAUSE)
84+
85+
if inst == "CAPSLOCK":
86+
kbd.send(Keycode.CAPS_LOCK)
87+
88+
if inst == "DELETE":
89+
kbd.send(Keycode.DELETE)
90+
91+
if inst == "ESC" or inst == "ESCAPE":
92+
kbd.send(Keycode.ESCAPE)
93+
94+
if inst == "HOME":
95+
kbd.send(Keycode.HOME)
96+
97+
if inst == "INSERT":
98+
kbd.send(Keycode.INSERT)
99+
100+
if inst == "NUMLOCK":
101+
kbd.send(Keycode.KEYPAD_NUMLOCK)
102+
103+
if inst == "PAGEUP":
104+
kbd.send(Keycode.PAGE_UP)
105+
106+
if inst == "PAGEDOWN":
107+
kbd.send(Keycode.PAGE_DOWN)
108+
109+
if inst == "PRINTSCREEN":
110+
kbd.send(Keycode.PRINT_SCREEN)
111+
112+
if inst == "SCROLLLOCK":
113+
kbd.send(Keycode.SCROLL_LOCK)
114+
115+
if inst == "SPACE":
116+
kbd.send(Keycode.SPACEBAR)
117+
118+
if inst == "TAB":
119+
kbd.send(Keycode.TAB)
120+
121+
if inst == "FN":
122+
print("FN Instruction can't be implimented, CircuitPython doesn't provide the macOS FN keycode, this is pretty useless anyway")
123+
124+
global previous_line
125+
if inst == "REPEAT":
126+
try:
127+
for _ in range(int(args[1])):
128+
process_line(previous_line)
129+
except ValueError:
130+
print("Invalid integer")
131+
return
132+
else:
133+
previous_line = line
134+
135+
for line in lines:
136+
process_line(line)

0 commit comments

Comments
 (0)