Skip to content

Commit 6c87b27

Browse files
committed
Begin and end keyboard only when sending keys. Delay 100 ms when sending combos.
1 parent c2a026a commit 6c87b27

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

keyboardv1/keyboardv1.ino

+29-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ void setup() {
1515

1616
Serial.begin(9600);
1717
Serial.println("Hello keyboard passthrough");
18-
// Keyboard.begin();
1918

2019
// Set the reset pin low for 10 ms.
2120
digitalWrite(resetPin, LOW);
@@ -117,6 +116,14 @@ void loop() {
117116
delayMicroseconds(baudDelay / 2);
118117

119118
char key = getChar();
119+
int theKey = (int)key;
120+
Serial.print("Raw char: ");
121+
Serial.print(key);
122+
Serial.print(" (decimal "); Serial.print(theKey); Serial.print(" =0b");
123+
for (int i = 7; i >= 0; i--) {
124+
Serial.write(bitRead(theKey, i));
125+
}
126+
Serial.println(")");
120127
sendChar(key);
121128
}
122129
}
@@ -136,15 +143,7 @@ char getChar() {
136143
delayMicroseconds(baudDelay);
137144
}
138145

139-
char keyChar = (char)theKey;
140-
Serial.print(keyChar);
141-
Serial.print(" (dec "); Serial.print(theKey); Serial.print(" =0b");
142-
for (int i = 7; i >= 0; i--) {
143-
Serial.write(bitRead(theKey, i));
144-
}
145-
Serial.println(")");
146-
147-
return keyChar;
146+
return (char) theKey;
148147
}
149148

150149
bool nextIsAlt = false;
@@ -154,24 +153,30 @@ void sendChar(char key) {
154153
// Figure out what to do with the key
155154
// * printable characters just get returned.
156155
// * control characters: ctrl + letter
157-
// * numpad needs to get translated
158-
// * HELP is ??? (192)
159-
// * F1 (193)-F12 (204) need to be translated
160156
// * F13 is "send next char as "alt""
161157
// * F14 is "numLock toggle"
158+
// * Other characters are translated.
162159
if (key >= ' ' && i <= '~') {
163160
if (nextIsAlt) {
164161
Serial.print("Alt+"); Serial.println(key);
162+
// Keyboard.begin();
165163
// Keyboard.press(KEY_ALT)
166164
// Keyboard.press(key);
165+
// delay(100);// I've seen this elsewhere. sometimes 20 ms
167166
// Keyboard.releaseAll();
167+
// Keyboard.end();
168168
nextIsAlt = false;
169169
return;
170170
}
171+
171172
Serial.print("Printable: "); Serial.println(key);
173+
// Keyboard.begin();
174+
// Keyboard.write(key);
175+
// Keyboard.end();
172176
return;
173177
}
174178

179+
// TODO: think about alt+ctrl+char, and alt+ctrl+shift+char
175180
char translated;
176181
if (numLock) {
177182
translated = numLockTable[key];
@@ -188,16 +193,24 @@ void sendChar(char key) {
188193
Serial.print("F14: Toggling numlock "); Serial.println(numLock);
189194
} else if (key < ' ') {
190195
Serial.print("ctrl-"); Serial.println(key+64);
196+
// Keyboard.begin();
191197
// Keyboard.press(KEY_LEFT_CTRL);
192198
// Keyboard.press(key + 64)
199+
// delay(100);// I've seen this elsewhere. sometimes 20 ms
193200
// Keyboard.releaseAll();
201+
// Keyboard.end();
194202
}
195203
} else if (translated >= ' ' && translated <= '~') {
196-
Serial.print("Printable: "); Serial.print(translated); Serial.print("; was: "); Serial.print((int) key);
197-
// Keyboard.print(translated);
204+
Serial.print("Printable: "); Serial.print(translated); Serial.print("; was decimal: "); Serial.print((int) key);
205+
// Keyboard.begin();
206+
// Keyboard.write(translated);
207+
// Keyboard.end();
198208
} else {
199-
Serial.print("Unprintable: "); Serial.print((int) translated); Serial.print("; was: "); Serial.print((int) key);
209+
Serial.print("Unprintable: "); Serial.print((int) translated); Serial.print(" decimal; was decimal: "); Serial.print((int) key);
210+
// Keyboard.begin();
200211
// Keyboard.press(key);
212+
// delay(100);// I've seen this elsewhere. sometimes 20 ms
201213
// Keyboard.releaseAll();
214+
// Keyboard.end();
202215
}
203216
}

0 commit comments

Comments
 (0)