Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

TinyUSB use Keyboard Modifier + Mouse Scroll Wheel
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by JorgenVikingGod on Sat Apr 10, 2021 1:16 pm

Hi Adafruit Forum,
can anyone help me to send CTRL (modifier) + Mouse Scroll Wheel (up/down) with TinyUSB?

There are lot of examples for using Keyboard or Mouse, but no one to combine both.

I create a simple macro pad to improve my CAD workflow.
There are some shortcuts for navigation, like SHIFT+ScrollWheel or CTRL+ScrollWheel...

Hope someone can give me some hints.

Thanks and cheers,
Jorgen

JorgenVikingGod
 
Posts: 7
Joined: Sat Apr 10, 2021 1:09 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by JorgenVikingGod on Sun Apr 18, 2021 10:56 am

Nobody any clue how to use TinyUSB?

JorgenVikingGod
 
Posts: 7
Joined: Sat Apr 10, 2021 1:09 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by mikeysklar on Mon Apr 19, 2021 4:28 pm

It looks like this example from a non Adafruit github repo might provide some guidance for combining up to six key / mouse selections at once.

https://github.com/cyborg5/TinyUSB_Mous ... Keyboard.h

There is also a BLE companion library to take a look at:

https://github.com/cyborg5/BLE52_Mouse_and_Keyboard

Just curious what did your custom input macropad device end up looking like?

mikeysklar
 
Posts: 4159
Joined: Mon Aug 01, 2016 8:10 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by todbot on Mon Apr 19, 2021 6:11 pm

Hi!
Here's an example that I think does what you talking about tinyusb_hid_ctrl_mousescroll.ino
It presses the Ctrl key and scrolls up on button press.

To do this, the device must be a USB HID Composite device, emulating a keyboard and mouse. There is an example in the Adafruit_TinyUSB_Arduino library that does most of this called "hid_composite.ino"

Here's the code in its entirety if you can't get to github:
Code: Select all | TOGGLE FULL SIZE
/*
  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;
  }
}

todbot
 
Posts: 12
Joined: Thu Mar 26, 2009 1:42 am

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by hathach on Mon Apr 19, 2021 8:55 pm

it is relatively very simple, if you understand how the HID report works. HID reports work like real actual hardware. How do you do Shift + Mouse Scroll
1. need to press shift -> send keyboardReport() with shift as modifier
2. mouse scroll --> send mouseScroll()
3. more scroll --> send more mouseScroll()
4. done --> send keyboardRelease() to release the shift key

The tricky part is you have to want for hid to by ready() before sending report because each report can only carry 1 report ID (keyboard or mouse). For actually API checkout https://github.com/adafruit/Adafruit_Ti ... _HID.h#L60

hathach
 
Posts: 1141
Joined: Tue Apr 23, 2013 1:02 am

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by JorgenVikingGod on Tue Apr 20, 2021 4:52 am

Wow! You guys are awesome, thanks for the detailed input.
I try to adapt this into my code.

It really looks easier than I thought at first.

And I like this lib to encapsulate the perfect TinyUSB into compatibility of "Keyboard.h" and "Mouse.h":
https://github.com/cyborg5/TinyUSB_Mous ... yboard.cpp

JorgenVikingGod
 
Posts: 7
Joined: Sat Apr 10, 2021 1:09 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by JorgenVikingGod on Tue Apr 20, 2021 5:27 am

mikeysklar wrote:Just curious what did your custom input macropad device end up looking like?


My little project is a small keyboard with 11 mechanical keys, 1 rotary encoder, small IPS display and WiFi (why not)
It was my first pcb project to learn KiCad (all sources coming online soon)

I forked the WiFiDuck project which pass over macros from an ESP8266 to and Arduino pro micro.
In my case I adapted the code to use the ESP32 as main MCU and build up multiple profiles too...
And add mechanical keys as input.

So you can map the keys with single action or complete sequences (=macros) by duckyscript.

In Attachment is a image of my current board with base plate pcb

WiFiDuck32_front.png
WiFiDuck32_front.png (897.96 KiB) Viewed 156 times

WiFiDuck32_back.png
WiFiDuck32_back.png (927.36 KiB) Viewed 156 times

JorgenVikingGod
 
Posts: 7
Joined: Sat Apr 10, 2021 1:09 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by mikeysklar on Tue Apr 20, 2021 10:42 am

@JorgenVikingGod,

Glad you liked Cyborg5's libs.

Love your build. We need to feature this!

mikeysklar
 
Posts: 4159
Joined: Mon Aug 01, 2016 8:10 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by JorgenVikingGod on Wed Apr 21, 2021 12:11 pm

Thx @mikeysklar - as soon the big issues solved I will push everything online.
Software and hardware will be open source and under MIT license.

First revision of the board is designed to use common devboards to solder on and everything is easy to hand solder.
I also want to make a revision 2 with an SAMD11 or SAMD21 as "keyboard" chip connected to the ESP32 (maybe put a raw ESP32 module on instead of a complete devboard)

Btw. advantage of using the ESP32 as brain of these keyboard is, that you can configure everything by a simple website (hosted by ESP32)

Screenshot from 2021-04-21 17-56-44.png
Multiple profiles configurable
Screenshot from 2021-04-21 17-56-44.png (90.72 KiB) Viewed 138 times


Screenshot from 2021-04-21 17-51-16.png
Define each key (colors and and macros)
Screenshot from 2021-04-21 17-51-16.png (160.44 KiB) Viewed 138 times


Screenshot from 2021-04-21 17-55-56.png
Clickable script words and help
Screenshot from 2021-04-21 17-55-56.png (269.87 KiB) Viewed 138 times

JorgenVikingGod
 
Posts: 7
Joined: Sat Apr 10, 2021 1:09 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by mikeysklar on Thu Apr 22, 2021 3:06 pm

Awesome pics. I had not seen the ESP32 web setting interface before. Please drop a note here when your go public with the project.

mikeysklar
 
Posts: 4159
Joined: Mon Aug 01, 2016 8:10 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by JorgenVikingGod on Fri Apr 23, 2021 11:12 am

Sure I will post it here. Maybe I bring it online this weekend without the TinyUSB migration and the work is done on extra branch.

JorgenVikingGod
 
Posts: 7
Joined: Sat Apr 10, 2021 1:09 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by JorgenVikingGod on Sat Apr 24, 2021 4:01 am

First draft is now online:
https://github.com/Jorgen-VikingGod/WiFiDuck32

ToDo:
  • adapt README.md (current state is from the fork base of WiFiDuck)
  • add async WiFiManager to configure WiFi network
  • add AP mode for the ESP32
  • modify atmega source to use TinyUSB
  • adapt the web interface to make it more robust for editing
  • maybe use display and rotary encoder to build up an on screen menu for configure WiFi

Hints:

Future:
  • board pcb revision 2 could use Type-C Seeeduino XIAO Microcontroller (https://www.aliexpress.com/item/1005001728616918.html) instead of Atmega32U4 Pro Micro
  • board pcb revision 3 could use SAMD11 or SAMD21 MCU directly on board instead of solder devboards on (because it did not need external cristal for USB use)
  • board pcb revision 3 could use ESP32 module directly on board instead of solder devboards on (saves lot of space)
  • plate pcb can breakout the profile next/previous buttons instead have them on the side
  • 3D printed housing between the pcbs

I will create a new Thread here with the current state of the project
(current state of the project is already working great, I'm using it for improve my game play and CAD tools)

Contribution is warmly welcome!

JorgenVikingGod
 
Posts: 7
Joined: Sat Apr 10, 2021 1:09 pm

Re: TinyUSB use Keyboard Modifier + Mouse Scroll Wheel

by mikeysklar on Mon Apr 26, 2021 10:21 am

A new thread would be great to keep up with the updates. Thank you for the share. Very informative.

mikeysklar
 
Posts: 4159
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.