Skip to content

Commit 1dfd9eb

Browse files
Add HID consumer (aka media) keys for USB, BT (earlephilhower#1309)
Allow sending thigns like KEY_MUTE or KEY_SCAN_NEXT from the USB and Bluetooth Classic keyboard libraries. BLE requires some add'l magic, not yet discovered. Pull in latest upstream Keyboard library changes
1 parent 3dbe5cf commit 1dfd9eb

File tree

9 files changed

+39
-5
lines changed

9 files changed

+39
-5
lines changed

cores/rp2040/RP2040USB.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void __SetupDescHIDReport() {
157157
//allocate memory for the HID report descriptors. We don't use them, but need the size here.
158158
uint8_t desc_hid_report_mouse[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1)) };
159159
uint8_t desc_hid_report_joystick[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(1)) };
160-
uint8_t desc_hid_report_keyboard[] = { TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)) };
160+
uint8_t desc_hid_report_keyboard[] = { TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)), TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(2)) };
161161
int size = 0;
162162

163163
//accumulate the size of all used HID report descriptors
@@ -194,7 +194,7 @@ void __SetupDescHIDReport() {
194194
if (__USBInstallMouse) {
195195
//determine if we need an offset (USB keyboard is installed)
196196
if (__USBInstallKeyboard) {
197-
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(2)) };
197+
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(3)) };
198198
memcpy(__hid_report + sizeof(desc_hid_report_keyboard), desc_local, sizeof(desc_local));
199199
} else {
200200
memcpy(__hid_report, desc_hid_report_mouse, sizeof(desc_hid_report_mouse));
@@ -206,7 +206,7 @@ void __SetupDescHIDReport() {
206206
uint8_t reportid = 1;
207207
int offset = 0;
208208
if (__USBInstallKeyboard) {
209-
reportid++;
209+
reportid += 2;
210210
offset += sizeof(desc_hid_report_keyboard);
211211
}
212212
if (__USBInstallMouse) {

libraries/Keyboard/examples/KeyboardPassword/KeyboardPassword.ino

+12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@ void setup() {
2626
void loop() {
2727
if (BOOTSEL) {
2828
Serial.println("Typing password for you...shhhh....");
29+
30+
Serial.println("First, mute the computer to be extra quiet");
31+
Keyboard.consumerPress(KEY_MUTE);
32+
delay(100);
33+
Keyboard.consumerRelease();
34+
2935
Keyboard.print("ThisPasswordIsWeakLikeABaby");
36+
37+
Serial.println("OK, unmute the computer since our secret is done");
38+
Keyboard.consumerPress(KEY_MUTE);
39+
delay(100);
40+
Keyboard.consumerRelease();
41+
3042
while (BOOTSEL);
3143
}
3244
}

libraries/Keyboard/src/Keyboard.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ void Keyboard_::sendReport(KeyReport* keys) {
4646
tud_task();
4747
}
4848

49+
void Keyboard_::sendConsumerReport(uint16_t key) {
50+
CoreMutex m(&__usb_mutex);
51+
tud_task();
52+
if (tud_hid_ready()) {
53+
tud_hid_report(__USBGetKeyboardReportID() + 1, &key, sizeof(key));
54+
}
55+
tud_task();
56+
}
57+
58+
4959
extern "C" void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
5060
(void) report_id;
5161
(void) instance;

libraries/Keyboard/src/Keyboard.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
class Keyboard_ : public HID_Keyboard {
2929
protected:
3030
virtual void sendReport(KeyReport* keys) override;
31+
virtual void sendConsumerReport(uint16_t key) override;
3132

3233
public:
3334
Keyboard_(void);

libraries/KeyboardBLE/src/KeyboardBLE.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ void KeyboardBLE_::sendReport(KeyReport* keys) {
6363
PicoBluetoothBLEHID.send(&data, sizeof(data));
6464
}
6565

66+
void KeyboardBLE_::sendConsumerReport(uint16_t key) {
67+
(void) key;
68+
// TODO - Need some BLE-specific code to send 2nd report
69+
}
70+
6671
KeyboardBLE_ KeyboardBLE;

libraries/KeyboardBLE/src/KeyboardBLE.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
class KeyboardBLE_ : public HID_Keyboard {
2929
private:
3030
virtual void sendReport(KeyReport* keys) override;
31+
virtual void sendConsumerReport(uint16_t key) override;
3132
public:
3233
KeyboardBLE_(void);
3334
void begin(const char *localName = nullptr, const char *hidName = nullptr, const uint8_t *layout = KeyboardLayout_en_US);

libraries/KeyboardBT/src/KeyboardBT.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ KeyboardBT_::KeyboardBT_(void) {
3434

3535
#define REPORT_ID 0x01
3636

37-
static const uint8_t desc_keyboard[] = {TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(REPORT_ID))};
37+
static const uint8_t desc_keyboard[] = {TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(REPORT_ID)), TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(REPORT_ID + 1))};
3838

3939
static void _hidReportCB(uint16_t cid, hid_report_type_t report_type, uint16_t report_id, int report_size, uint8_t *report) {
4040
(void) cid;
@@ -71,4 +71,8 @@ void KeyboardBT_::sendReport(KeyReport* keys) {
7171
PicoBluetoothHID.send(REPORT_ID, &data, sizeof(data));
7272
}
7373

74+
void KeyboardBT_::sendConsumerReport(uint16_t key) {
75+
PicoBluetoothHID.send(REPORT_ID + 1, &key, sizeof(key));
76+
}
77+
7478
KeyboardBT_ KeyboardBT;

libraries/KeyboardBT/src/KeyboardBT.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
class KeyboardBT_ : public HID_Keyboard {
2929
protected:
3030
virtual void sendReport(KeyReport* keys) override;
31+
virtual void sendConsumerReport(uint16_t key) override;
3132
public:
3233
KeyboardBT_(void);
3334
void begin(const char *localName = nullptr, const char *hidName = nullptr, const uint8_t *layout = KeyboardLayout_en_US);

0 commit comments

Comments
 (0)