Author Topic: Question about a USB keyboard emulator.  (Read 1358 times)

H3nk

  • Newbie
  • *
  • Posts: 1
Question about a USB keyboard emulator.
« on: February 09, 2016, 02:24:50 am »
I've built this thingy:http://www.practicalarduino.com/projects/virtual-usb-keyboard
and it works a treat, but I want to send a character with 2 control codes, and that doesn't work.
For example shift + ctrl + x.
Code: [Select]
UsbKeyboard.sendKeyStroke(KEY_B,MOD_CONTROL_LEFT) works, but
UsbKeyboard.sendKeyStroke(KEY_B,MOD_CONTROL_LEFT, KEY_ARROW_LEFT)
does not work, and I get the following error message:

UsbKeyboardtest.ino: In function 'void loop()':
UsbKeyboardtest:41: error: no matching function for call to 'UsbKeyboardDevice::sendKeyStroke(int, int, int)'
D:\arduino-1.0.5-r2\libraries\UsbKeyboard/UsbKeyboard.h:153: note: candidates are: void UsbKeyboardDevice::sendKeyStroke(byte)
D:\arduino-1.0.5-r2\libraries\UsbKeyboard/UsbKeyboard.h:157: note:                 void UsbKeyboardDevice::sendKeyStroke(byte, byte)

I think it's got something to do with the following in Usbkeyboard.h:
Code: [Select]
void sendKeyStroke(byte keyStroke) {
    sendKeyStroke(keyStroke, 0);
  }

  void sendKeyStroke(byte keyStroke, byte modifiers) {
     
    while (!usbInterruptIsReady()) {
      // Note: We wait until we can send keystroke
      //       so we know the previous keystroke was
      //       sent.
    }
     
    memset(reportBuffer, 0, sizeof(reportBuffer));

    reportBuffer[0] = modifiers;
    reportBuffer[1] = keyStroke;
       

I have already tried a few things, but my knowledge of C++ is very minimal, so it doesn't surprise me that it doesn't work. I need to add an extra modifier.
The only thing I want, is to send a ctrl + uppercase character, but since the HID tables only has lowercase, I need to send a shift too, and that's where it goes wrong.

I have only posted snippets of the code, I assume that others are in the know about this project.
I have also found another Usbkeyboard.h where the press command is implemented, but that also doesn't work.
In some way I'll have to add an extra byte modifier to the following line, but what I try, I only get errors. >:(
Code: [Select]
void sendKeyStroke(byte keyStroke, byte modifiers)
Thanks in advance for helping me out.

Ralf

  • Full Member
  • ***
  • Posts: 152
Re: Question about a USB keyboard emulator.
« Reply #1 on: February 11, 2016, 01:20:16 am »
Hi H3nk,

you may add several modifieres into one byte, as they only are bits.

this is from USBkeyboard.h:
Code: [Select]
#define MOD_CONTROL_LEFT    (1<<0)
#define MOD_SHIFT_LEFT      (1<<1)
#define MOD_ALT_LEFT        (1<<2)
#define MOD_GUI_LEFT        (1<<3)
#define MOD_CONTROL_RIGHT   (1<<4)
#define MOD_SHIFT_RIGHT     (1<<5)
#define MOD_ALT_RIGHT       (1<<6)
#define MOD_GUI_RIGHT       (1<<7)

In decimal, MOD_CONTROL_LEFT is 1,  MOD_SHIFT_LEFT is 2,  MOD_ALT_LEFT  is 4,  MOD_GUI_LEFT is 8, and so on.

So, shift + ctrl + x would be:

Code: [Select]
UsbKeyboard.sendKeyStroke(KEY_X, 1+2)
or more elegant:
UsbKeyboard.sendKeyStroke(KEY_X, MOD_CONTROL_LEFT + MOD_SHIFT_LEFT)

But your second one
Code: [Select]
UsbKeyboard.sendKeyStroke(KEY_B,MOD_CONTROL_LEFT, KEY_ARROW_LEFT)

violates the USB keyboard standard, because KEY_ARROW_LEFT is no modifier.

Ralf