Skip to content

Commit 66c89ab

Browse files
authored
Merge pull request #2 from gitdev-bash/Keymaps-and-Input
Keymap, Input and other small fixes
2 parents 43182dd + 96bc415 commit 66c89ab

File tree

4 files changed

+229
-93
lines changed

4 files changed

+229
-93
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
Piduck is a program with which you can easily use your Raspberry Pi Zero as a USB HID keyboard. It uses the scripting language [Ducky-Script-v1].
33

44
The Project is in Beta and help is needed.
5-
The function `REPEAT`, Control keys and other special character and keys are missing. For now, only the US keyboard layout is available.
5+
The function `REPEAT`, `FN` and other special character and keys are missing. For now, only the US keyboard layout is available.
66

77
## Contributions
88
All contributions are welcome!
9-
109
## Setup
1110
### HID Setup
1211
[Source1][Hid-setup-source1]
@@ -70,10 +69,11 @@ Also run these commands as root!
7069
Syntax of [Ducky-Script-v1][Ducky-Script-v1-Syntax]
7170
Root is needed (again)
7271
### Using Script File
73-
`piduck inject.txt`
72+
`piduck -i inject.txt`
7473
### Input from standard input
7574
`piduck`
76-
75+
### Set Layout
76+
`piduck -l xx`
7777
## Disclamer
7878
This project may not be used illegally and i am not responsible for any damages made with or by this project.
7979

pd_key_maps/keymap_us.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/bin/python3
2+
commap = {
3+
"LCTRL": 1,
4+
"LSHIFT": 2,
5+
"LALT": 4,
6+
"LMETA": 8,
7+
"RCTRL": 16,
8+
"RSHIFT": 32,
9+
"RALT": 64,
10+
"RMETA": 128,
11+
}
12+
c1map = {
13+
"a": 4,
14+
"b": 5,
15+
"c": 6,
16+
"d": 7,
17+
"e": 8,
18+
"f": 9,
19+
"g": 10,
20+
"h": 11,
21+
"i": 12,
22+
"j": 13,
23+
"k": 14,
24+
"l": 15,
25+
"m": 16,
26+
"n": 17,
27+
"o": 18,
28+
"p": 19,
29+
"q": 20,
30+
"r": 21,
31+
"s": 22,
32+
"t": 23,
33+
"u": 24,
34+
"v": 25,
35+
"w": 26,
36+
"x": 27,
37+
"y": 28,
38+
"z": 29,
39+
"1": 30,
40+
"2": 31,
41+
"3": 32,
42+
"4": 33,
43+
"5": 34,
44+
"6": 35,
45+
"7": 36,
46+
"8": 37,
47+
"9": 38,
48+
"0": 39,
49+
"RETURN": 40,
50+
"ESC": 41,
51+
"BACKSPACE": 42,
52+
"TAB": 43,
53+
"SPACE": 44,
54+
"-": 45,
55+
"=": 46,
56+
"(": 47,
57+
")": 48,
58+
"\\": 49,
59+
"#": 50,
60+
":": 51,
61+
'"': 52,
62+
"~": 53,
63+
",": 54,
64+
".": 55,
65+
"/": 56,
66+
"CAPSLOCK": 57,
67+
"F1": 58,
68+
"F2": 59,
69+
"F3": 60,
70+
"F4": 61,
71+
"F5": 62,
72+
"F6": 63,
73+
"F7": 64,
74+
"F8": 65,
75+
"F9": 66,
76+
"F10": 67,
77+
"F11": 68,
78+
"F12": 69,
79+
"PRINT": 70,
80+
"SCROLLLOCK": 71,
81+
"PAUSE": 72,
82+
"INSERT": 73,
83+
"HOME": 74,
84+
"PAGEUP": 75,
85+
"DELETE": 76,
86+
"END": 77,
87+
"PAGEDOWN": 78,
88+
"RIGHT": 79,
89+
"LEFT": 80,
90+
"DOWN": 81,
91+
"UP": 82,
92+
"NUMLOCK": 83,
93+
# kp-divide:"54",
94+
# kp-multiply:"55",
95+
# kp-minus:"56",
96+
# kp-plus:"57",
97+
# kp-return:"58",
98+
# kp-1:"59",
99+
# kp-2:"5a",
100+
# kp-3:"5b",
101+
# kp-4:"5c",
102+
# kp-5:"5d",
103+
# kp-6:"5e",
104+
# kp-7:"5f",
105+
# kp-8:"60",
106+
# kp-9:"61",
107+
# kp-0:"62",
108+
# kp-period:"63",
109+
# application:"65",
110+
# power:"66",
111+
# kp-equal:"67",
112+
"F13": 104,
113+
"F14": 105,
114+
"F15": 106,
115+
"F16": 107,
116+
"F17": 108,
117+
"F18": 109,
118+
"F19": 110,
119+
"F20": 111,
120+
"F21": 112,
121+
"F22": 113,
122+
"F23": 114,
123+
"F24": 115,
124+
# execute:116,
125+
# help:117,
126+
# menu:118,
127+
# select:119,
128+
# cancel:120,
129+
# redo:121,
130+
# undo:122,
131+
# cut:123,
132+
# copy:124,
133+
# paste:125,
134+
# find:126,
135+
# mute:127,
136+
# volume-up:128,
137+
# volume-down:129,
138+
}
139+
c2map = {}
140+
for i in range(65, 91):
141+
c2map[chr(i)] = "SHIFT " + chr(i).lower()
142+
aliasmap = {
143+
"CTRL": "LCTRL",
144+
"SHIFT": "LSHIFT",
145+
"ALT": "LALT",
146+
"META": "LMETA",
147+
"CONTROL": "CTRL",
148+
"GUI": "META",
149+
"ESCAPE": "ESC",
150+
"RIGHTARROW": "RIGHT",
151+
"LEFTARROW": "LEFT",
152+
"DOWNARROW": "DOWN",
153+
"UPARROW": "UP",
154+
"CTRL-ALT": "CTRL ALT",
155+
"CTRL-SHIFT": "CTRL SHIFT",
156+
"DEFAULT_DELAY": "DEFAULTDELAY",
157+
" ": "SPACE",
158+
"BREAK": "PAUSE",
159+
}

piduck.py

Lines changed: 65 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,116 @@
11
#!/bin/python3
2-
import sys
2+
# import sys
3+
import argparse
4+
from importlib import import_module
35
from time import sleep
46

5-
commap = {}
6-
c1map = {
7-
"a": 4,
8-
"b": 5,
9-
"c": 6,
10-
"d": 7,
11-
"e": 8,
12-
"f": 9,
13-
"g": 10,
14-
"h": 11,
15-
"i": 12,
16-
"j": 13,
17-
"k": 14,
18-
"l": 15,
19-
"m": 16,
20-
"n": 17,
21-
"o": 18,
22-
"p": 19,
23-
"q": 20,
24-
"r": 21,
25-
"s": 22,
26-
"t": 23,
27-
"u": 24,
28-
"v": 25,
29-
"w": 26,
30-
"x": 27,
31-
"y": 28,
32-
"z": 29,
33-
"1": 30,
34-
"2": 31,
35-
"3": 32,
36-
"4": 33,
37-
"5": 34,
38-
"6": 35,
39-
"7": 36,
40-
"8": 37,
41-
"9": 38,
42-
"0": 39,
43-
"RETURN": 40,
44-
"ESC": 41,
45-
"BACKSPACE": 42,
46-
"TAB": 43,
47-
"SPACE": 44,
48-
}
49-
c2map = {}
50-
for i in range(65, 91):
51-
c2map[chr(i)] = "SHIFT " + chr(i).lower()
52-
aliasmap = {
53-
"CTRL-ALT": "CTRL ALT",
54-
"CTRL-SHIFT": "CTRL SHIFT",
55-
"DEFAULT_DELAY": "DEFAULTDELAY",
56-
}
7+
key_layout = "us"
578
default_delay = 10
58-
string_delay = 10
9+
string_delay = 1
5910

60-
61-
def divide_chunks(l, n):
62-
for i in range(0, len(l), n):
63-
yield l[i : i + n]
11+
piparser = argparse.ArgumentParser()
12+
piparser.add_argument("-i", "--input", help="File input")
13+
piparser.add_argument(
14+
"-l", "--keyboardlayoutcode", help="Language codes specified by ISO639-1:2002"
15+
)
16+
piparser.add_argument("-d", "--defaultdelay", help="The default delay of execution")
17+
piparser.add_argument(
18+
"-s", "--defaultchardelay", help="The default char delay of execution"
19+
)
20+
piargs = piparser.parse_args()
21+
if piargs.keyboardlayoutcode is not None:
22+
key_layout = piargs.keyboardlayoutcode
23+
if piargs.defaultdelay is not None:
24+
default_delay = piargs.defaultdelay
25+
if piargs.defaultchardelay is not None:
26+
string_delay = piargs.defaultchardelay
27+
try:
28+
keymap = import_module("pd_key_maps.keymap_" + key_layout)
29+
except ModuleNotFoundError:
30+
exit(3)
6431

6532

6633
def string(string):
6734
for char in string:
68-
pharse(char, [], True)
35+
pharse(char, [[], []], True)
6936
sleep(string_delay / 100)
7037

7138

7239
def pharse(line, known, deltrue):
7340
global default_delay
7441
global string_delay
75-
command = line.split()
76-
if not deltrue:
77-
if command[0] == "DELAY":
78-
sleep(int(command[1]) / 100)
79-
return
80-
elif command[0] == "REM":
81-
return
82-
elif command[0] == "REPEAT":
83-
return # todo
84-
else:
85-
sleep(default_delay / 100)
86-
if command[0] == "STRING":
87-
string(" ".join(command[1:]))
42+
if line != " ":
43+
command = line.split()
44+
else:
45+
command = [" "]
46+
if command[0] == "DELAY":
47+
sleep(int(command[1]) / 100)
48+
return
49+
elif command[0] == "REM":
8850
return
51+
elif command[0] == "REPEAT":
52+
return # todo
8953
elif command[0] == "DEFAULTCHARDELAY":
9054
string_delay = int(command[1])
9155
return
9256
elif command[0] == "DEFAULTDELAY":
9357
default_delay = int(command[1])
9458
return
95-
elif command[0] in aliasmap:
96-
pharse(aliasmap[command[0]] + " ".join(command[1:]), True)
59+
else:
60+
if not deltrue:
61+
sleep(default_delay / 100)
62+
if command[0] == "STRING":
63+
string(line[len("STRING ") :])
9764
return
98-
elif command[0] in commap:
99-
known.append(commap[command[0]])
65+
elif command[0] in keymap.commap:
66+
known[0].append(keymap.commap[command[0]])
10067
pharse(" ".join(command[1:]), known, True)
10168
return
102-
elif command[0] in c1map:
103-
known.append(c1map[command[0]])
69+
elif command[0] in keymap.c1map:
70+
known[1].append(keymap.c1map[command[0]])
10471
out(known)
10572
return
73+
elif command[0] in keymap.c2map:
74+
pharse(keymap.c2map[command[0]], known, True)
75+
return
76+
elif command[0] in keymap.aliasmap:
77+
pharse(keymap.aliasmap[command[0]] + " " + " ".join(command[1:]), known, True)
78+
return
10679
else:
10780
exit(2)
10881

10982

11083
def out(ccl):
111-
# ccl_part=list(divide_chunks(ccl, n))
112-
rep = chr(0) * 2
113-
i = 2
114-
for e in ccl:
115-
i += 1
84+
rep = ""
85+
if len(ccl[0]) > 0:
86+
for e in ccl[0]:
87+
rep = rep + chr(e)
88+
else:
89+
rep = rep + chr(0)
90+
rep = rep + chr(0)
91+
for e in ccl[1]:
11692
rep = rep + chr(e)
117-
rep = rep + (chr(0) * (8 - i))
93+
rep = rep + chr(0) * (8 - len(rep))
11894
with open("/dev/hidg0", "rb+") as fd:
11995
fd.write(rep.encode())
12096
fd.write((chr(0) * 8).encode())
12197

12298

12399
def main():
124-
if len(sys.argv) >= 2:
125-
file1 = open(sys.argv[1], "r")
100+
if piargs.input is not None:
101+
file1 = open(piargs.input, "r")
126102
while True:
127103
line = file1.readline()
128104
if not line:
129105
break
130-
pharse(line.strip(), [], False)
106+
pharse(line.strip(), [[], []], False)
131107
file1.close()
132-
elif len(sys.argv) == 1:
108+
else:
133109
while True:
134110
line = input()
135111
if not line:
136112
break
137-
pharse(line.strip(), [], False)
113+
pharse(line.strip(), [[], []], False)
138114

139115

140116
main()

0 commit comments

Comments
 (0)