Skip to content

Commit 5bdf03b

Browse files
committed
USBKeyboard: add key_code_raw API
Fixes #578
1 parent 949c70c commit 5bdf03b

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "PluggableUSBHID.h"
2+
#include "USBKeyboard.h"
3+
4+
USBKeyboard Keyboard;
5+
6+
void setup() {
7+
// put your setup code here, to run once:
8+
}
9+
10+
// raw keycodes can be imported from here https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2#file-usb_hid_keys-h
11+
#define KEY_ESC 0x29 // Keyboard ESCAPE
12+
#define KEY_I 0x0c // Keyboard i and I
13+
14+
void loop() {
15+
// in vim, jump between INSERT and COMMAND mode
16+
delay(1000);
17+
Keyboard.key_code_raw(KEY_ESC);
18+
delay(1000);
19+
Keyboard.key_code_raw(KEY_I);
20+
}

Diff for: libraries/USBHID/src/USBKeyboard.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ int USBKeyboard::_putc(int c)
463463
}
464464

465465
bool USBKeyboard::key_code(uint8_t key, uint8_t modifier)
466+
{
467+
return key_code_raw(keymap[key].usage, modifier);
468+
}
469+
470+
bool USBKeyboard::key_code_raw(uint8_t key, uint8_t modifier)
466471
{
467472
_mutex.lock();
468473

@@ -472,7 +477,7 @@ bool USBKeyboard::key_code(uint8_t key, uint8_t modifier)
472477
report.data[0] = REPORT_ID_KEYBOARD;
473478
report.data[1] = modifier;
474479
report.data[2] = 0;
475-
report.data[3] = keymap[key].usage;
480+
report.data[3] = key;
476481
report.data[4] = 0;
477482
report.data[5] = 0;
478483
report.data[6] = 0;

Diff for: libraries/USBHID/src/USBKeyboard.h

+14
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ class USBKeyboard: public USBHID, public ::mbed::Stream {
154154
*/
155155
bool key_code(uint8_t key, uint8_t modifier = 0);
156156

157+
/**
158+
* To send directly a keycode
159+
*
160+
* @code
161+
* //To send ESC
162+
* keyboard.key_code_raw(0x29);
163+
* @endcode
164+
*
165+
* @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
166+
* @param key character to send
167+
* @returns true if there is no error, false otherwise
168+
*/
169+
bool key_code_raw(uint8_t key, uint8_t modifier = 0);
170+
157171
/**
158172
* Send a character
159173
*

0 commit comments

Comments
 (0)