Skip to content

Commit 792a7fd

Browse files
committed
Refactor key press handling and add support for combo keys
1 parent 660de86 commit 792a7fd

File tree

2 files changed

+70
-89
lines changed

2 files changed

+70
-89
lines changed

src/helper/transpiler.cpp

Lines changed: 68 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,26 @@ bool transpiler::checkArgs(const std::vector<String>& args, const uint8_t expect
5959
}
6060

6161
void transpiler::processLine(const char* command, const std::vector<String>& args) {
62-
if (strcmp(command, "write") == 0) { // write "<text>"
63-
Serial.println( "write" );
62+
if (strcmp(command, "write") == 0) {
6463
if (!checkArgs(args, 1)) return;
64+
6565
Keyboard.print(args[0]);
6666
}
6767

68-
if (strcmp(command, "writeLn") == 0) { // writeLn "<text>"
68+
if (strcmp(command, "writeLn") == 0) {
6969
if (!checkArgs(args, 1)) return;
70+
7071
Keyboard.print(args[0]);
7172
Keyboard.press(KEY_RETURN);
7273
Keyboard.release(KEY_RETURN);
7374
}
7475

75-
if (strcmp(command, "delay") == 0) { // delay "<integer>"
76+
if (strcmp(command, "delay") == 0) {
7677
if (!checkArgs(args, 1)) return;
7778
delay(args[0].toInt());
7879
}
7980

80-
if (strcmp(command, "terminal") == 0) { // terminal "<operating_system>"
81+
if (strcmp(command, "terminal") == 0) {
8182
if (!checkArgs(args, 1)) return;
8283
if (args[0] == "windows") {
8384
Keyboard.press(KEY_LEFT_GUI);
@@ -107,88 +108,25 @@ void transpiler::processLine(const char* command, const std::vector<String>& arg
107108
}
108109
}
109110

110-
if (strcmp(command, "winShutdown") == 0)
111-
{
112-
Keyboard.press(KEY_LEFT_GUI);
113-
Keyboard.press('X');
114-
Keyboard.releaseAll();
115-
116-
delay(1000);
117-
118-
Keyboard.press('U');
119-
Keyboard.releaseAll();
120-
121-
Keyboard.press('U');
122-
Keyboard.releaseAll();
123-
}
124-
125-
if (strcmp(command, "key") == 0)
126-
{
111+
if (strcmp(command, "key") == 0) {
127112
if (!checkArgs(args, 1)) return;
128113

129-
if (args[0] == "enter") Keyboard.press(KEY_RETURN);
130-
else if (args[0] == "esc") Keyboard.press(KEY_ESC);
131-
else if (args[0] == "backspace") Keyboard.press(KEY_BACKSPACE);
132-
else if (args[0] == "tab") Keyboard.press(KEY_TAB);
133-
else if (args[0] == "space") Keyboard.press(' ');
134-
else if (args[0] == "delete") Keyboard.press(KEY_DELETE);
135-
else if (args[0] == "insert") Keyboard.press(KEY_INSERT);
136-
else if (args[0] == "home") Keyboard.press(KEY_HOME);
137-
else if (args[0] == "end") Keyboard.press(KEY_END);
138-
else if (args[0] == "pageup") Keyboard.press(KEY_PAGE_UP);
139-
else if (args[0] == "pagedown") Keyboard.press(KEY_PAGE_DOWN);
140-
else if (args[0] == "up") Keyboard.press(KEY_UP_ARROW);
141-
else if (args[0] == "down") Keyboard.press(KEY_DOWN_ARROW);
142-
else if (args[0] == "left") Keyboard.press(KEY_LEFT_ARROW);
143-
else if (args[0] == "right") Keyboard.press(KEY_RIGHT_ARROW);
144-
else if (args[0] == "ctrl") Keyboard.press(KEY_LEFT_CTRL);
145-
else if (args[0] == "shift") Keyboard.press(KEY_LEFT_SHIFT);
146-
else if (args[0] == "alt") Keyboard.press(KEY_LEFT_ALT);
147-
else if (args[0] == "gui") Keyboard.press(KEY_LEFT_GUI);
148-
else if (args[0] == "capslock") Keyboard.press(KEY_CAPS_LOCK);
149-
else if (args[0] == "numlock") Keyboard.press(KEY_NUM_LOCK);
150-
else if (args[0] == "prtsc") Keyboard.press(KEY_PRINT_SCREEN);
151-
else if (args[0] == "scrolllock") Keyboard.press(KEY_SCROLL_LOCK);
152-
else if (args[0] == "pause") Keyboard.press(KEY_PAUSE);
153-
else return;
114+
pressKey(args[0]);
154115

155116
Keyboard.releaseAll();
156117
}
157118

158-
if (strcmp(command, "functionKey") == 0) // functionKey "<F1-F24>"
159-
{
119+
if (strcmp(command, "comboKey") == 0) {
160120
if (!checkArgs(args, 1)) return;
161-
if (args[0] == "F1") Keyboard.press(KEY_F1);
162-
else if (args[0] == "F2") Keyboard.press(KEY_F2);
163-
else if (args[0] == "F3") Keyboard.press(KEY_F3);
164-
else if (args[0] == "F4") Keyboard.press(KEY_F4);
165-
else if (args[0] == "F5") Keyboard.press(KEY_F5);
166-
else if (args[0] == "F6") Keyboard.press(KEY_F6);
167-
else if (args[0] == "F7") Keyboard.press(KEY_F7);
168-
else if (args[0] == "F8") Keyboard.press(KEY_F8);
169-
else if (args[0] == "F9") Keyboard.press(KEY_F9);
170-
else if (args[0] == "F10") Keyboard.press(KEY_F10);
171-
else if (args[0] == "F11") Keyboard.press(KEY_F11);
172-
else if (args[0] == "F12") Keyboard.press(KEY_F12);
173-
else if (args[0] == "F13") Keyboard.press(KEY_F13);
174-
else if (args[0] == "F14") Keyboard.press(KEY_F14);
175-
else if (args[0] == "F15") Keyboard.press(KEY_F15);
176-
else if (args[0] == "F16") Keyboard.press(KEY_F16);
177-
else if (args[0] == "F17") Keyboard.press(KEY_F17);
178-
else if (args[0] == "F18") Keyboard.press(KEY_F18);
179-
else if (args[0] == "F19") Keyboard.press(KEY_F19);
180-
else if (args[0] == "F20") Keyboard.press(KEY_F20);
181-
else if (args[0] == "F21") Keyboard.press(KEY_F21);
182-
else if (args[0] == "F22") Keyboard.press(KEY_F22);
183-
else if (args[0] == "F23") Keyboard.press(KEY_F23);
184-
else if (args[0] == "F24") Keyboard.press(KEY_F24);
185-
else return;
186121

122+
std::vector<String> keys = split(args[0], '+');
123+
for (const String& key : keys) {
124+
pressKey(key);
125+
}
187126
Keyboard.releaseAll();
188127
}
189128

190-
if (strcmp(command, "arrowKey") == 0) // arrowKey "<up/down/left/right>"
191-
{
129+
if (strcmp(command, "arrowKey") == 0) {
192130
if (!checkArgs(args, 1)) return;
193131
if (args[0] == "up") Keyboard.press(KEY_UP_ARROW);
194132
else if (args[0] == "down") Keyboard.press(KEY_DOWN_ARROW);
@@ -199,19 +137,6 @@ void transpiler::processLine(const char* command, const std::vector<String>& arg
199137
Keyboard.releaseAll();
200138
}
201139

202-
if (strcmp(command, "clipboard") == 0) {
203-
if (!checkArgs(args, 1)) return;
204-
if (args[0] == "copy") {
205-
Keyboard.press(KEY_LEFT_CTRL);
206-
Keyboard.press('c');
207-
Keyboard.releaseAll();
208-
} else if (args[0] == "paste") {
209-
Keyboard.press(KEY_LEFT_CTRL);
210-
Keyboard.press('v');
211-
Keyboard.releaseAll();
212-
}
213-
}
214-
215140
if (strcmp(command, "keyboardLayout") == 0)
216141
{
217142
if (!checkArgs(args, 1)) return;
@@ -238,3 +163,57 @@ void transpiler::processLine(const char* command, const std::vector<String>& arg
238163
}
239164
}
240165

166+
std::vector<String> transpiler::split(const String& str, const char delimiter) {
167+
std::vector<String> tokens;
168+
String token = "";
169+
170+
for (size_t i = 0; i < str.length(); i++) {
171+
if (str[i] == delimiter) {
172+
if (token.length() > 0) {
173+
tokens.push_back(token);
174+
token = "";
175+
}
176+
} else {
177+
token += str[i];
178+
}
179+
}
180+
if (token.length() > 0) {
181+
tokens.push_back(token);
182+
}
183+
return tokens;
184+
}
185+
186+
void transpiler::pressKey(const String& key) {
187+
if (key == "enter") Keyboard.press(KEY_RETURN);
188+
else if (key == "esc") Keyboard.press(KEY_ESC);
189+
else if (key == "backspace") Keyboard.print(" ");
190+
else if (key == "tab") Keyboard.press(KEY_TAB);
191+
else if (key == "space") Keyboard.press(KEY_BACKSPACE);
192+
else if (key == "delete") Keyboard.press(KEY_DELETE);
193+
else if (key == "insert") Keyboard.press(KEY_INSERT);
194+
else if (key == "home") Keyboard.press(KEY_HOME);
195+
else if (key == "end") Keyboard.press(KEY_END);
196+
else if (key == "pageup") Keyboard.press(KEY_PAGE_UP);
197+
else if (key == "pagedown") Keyboard.press(KEY_PAGE_DOWN);
198+
else if (key == "up") Keyboard.press(KEY_UP_ARROW);
199+
else if (key == "down") Keyboard.press(KEY_DOWN_ARROW);
200+
else if (key == "left") Keyboard.press(KEY_LEFT_ARROW);
201+
else if (key == "right") Keyboard.press(KEY_RIGHT_ARROW);
202+
else if (key == "ctrl") Keyboard.press(KEY_LEFT_CTRL);
203+
else if (key == "shift") Keyboard.press(KEY_LEFT_SHIFT);
204+
else if (key == "alt") Keyboard.press(KEY_LEFT_ALT);
205+
else if (key == "gui") Keyboard.press(KEY_LEFT_GUI);
206+
else if (key == "capslock") Keyboard.press(KEY_CAPS_LOCK);
207+
else if (key == "numlock") Keyboard.press(KEY_NUM_LOCK);
208+
else if (key == "prtsc") Keyboard.press(KEY_PRINT_SCREEN);
209+
else if (key == "scrolllock") Keyboard.press(KEY_SCROLL_LOCK);
210+
else if (key == "pause") Keyboard.press(KEY_PAUSE);
211+
else if (key.startsWith("F")) {
212+
const int fKey = key.substring(1).toInt();
213+
if (fKey >= 1 && fKey <= 24) {
214+
Keyboard.press(KEY_F1 + (fKey - 1));
215+
}
216+
} else if (key.length() == 1) {
217+
Keyboard.press(key[0]);
218+
}
219+
}

src/helper/transpiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class transpiler {
1111
private:
1212
static void processLine(const char* command, const std::vector<String>& args);
1313
static bool checkArgs(const std::vector<String>& args, uint8_t expected);
14+
static void pressKey(const String& key);
15+
static std::vector<String> split(const String& str, char delimiter);
1416
};
1517

1618
extern CH9329_Keyboard_ Keyboard;

0 commit comments

Comments
 (0)