Last active
May 4, 2021 13:21
-
-
Save todbot/13880b3ddfb4f1c857328f1974b12036 to your computer and use it in GitHub Desktop.
Use TinyUSB to emulate keyboard + mouse to press Ctrl key and do mouse scroll at the same time. Tested on QT Py M0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
tinyusb_hid_ctrl_mousesscroll.ino | |
2021 @todbot | |
Modified example from | |
https://github.com/adafruit/Adafruit_TinyUSB_Arduino/blob/master/examples/HID/hid_composite/hid_composite.ino | |
This sketch demonstrates multiple report USB HID. | |
Press button pin will move | |
- press 'Ctrl' key | |
- mouse scroll up using scrollwheel | |
*/ | |
#include "Adafruit_TinyUSB.h" | |
const int buttonPin = 10; | |
bool activeState = false; | |
// Report IDs | |
enum | |
{ | |
RID_KEYBOARD = 1, | |
RID_MOUSE, | |
RID_CONSUMER_CONTROL, // Media, volume etc .. | |
}; | |
// HID report descriptor using TinyUSB's template | |
uint8_t const desc_hid_report[] = | |
{ | |
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD) ), | |
TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE) ), | |
TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(RID_CONSUMER_CONTROL) ) | |
}; | |
// USB HID object | |
Adafruit_USBD_HID usb_hid; | |
// the setup function runs once when you press reset or power the board | |
void setup() | |
{ | |
usb_hid.setPollInterval(2); | |
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); | |
//usb_hid.setStringDescriptor("TinyUSB HID Composite"); | |
usb_hid.begin(); | |
// Set up button, pullup opposite to active state | |
pinMode(buttonPin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP); | |
Serial.begin(115200); | |
Serial.println("Adafruit TinyUSB HID Composite Ctrl + Mouse Scroll example"); | |
// wait until device mounted | |
while ( !USBDevice.mounted() ) delay(1); | |
} | |
bool has_key = false; | |
void loop() | |
{ | |
// poll gpio once each 50 ms | |
// (and only send usb reports this fast) | |
delay(50); | |
if ( ! usb_hid.ready() ) { | |
return; | |
} | |
// Whether button is pressed | |
bool btn_pressed = (digitalRead(buttonPin) == activeState); | |
if ( btn_pressed ) { | |
Serial.println("Presss! Sending Ctrl + mouse scroll"); | |
// press CTRL on keyboard | |
uint8_t keycode[6] = {HID_KEY_NONE}; | |
usb_hid.keyboardReport(RID_KEYBOARD, KEYBOARD_MODIFIER_LEFTCTRL, keycode); | |
has_key = true; | |
// delay a bit before attempt to send mouse report | |
delay(10); | |
// move scroll | |
int8_t scroll_amount = 1; | |
usb_hid.mouseScroll(RID_MOUSE, scroll_amount, 0); | |
} | |
else { | |
// release key if was pressed | |
if (has_key) { | |
Serial.println("Release!"); | |
usb_hid.keyboardRelease(RID_KEYBOARD); | |
} | |
has_key = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment