Andrey Pro
Andrey Pro

Reputation: 501

Arduino: need assistance in understanding <keyboard.h> library

I have Leonardo/Micro device that should emulate Keyboard. I would like to modify library. The reason is I would like to be able to send raw scancodes, wheras the library does some preparation. I looked in the source code, also of HID library, dbut have difficulty to understand following points:

  1. Keyboard_::begin() and Keyboard_::end() are supposed to start and stop keboard emulation, but they have empty bodies; https://www.arduino.cc/en/Reference/KeyboardBegin
  2. KeyReport is especially mysterious:
  3. What exactly happens to the keyreport? I lost track in USB_Send function in HID.cpp. Couldnt find where it comes from
  4. What are modifiers, what they ar4 doing?
  5. Is number of keys sent limited to 6 or, theoretically could be be arbitrary?

Upvotes: 0

Views: 2147

Answers (1)

Matthew Heironimus
Matthew Heironimus

Reputation: 356

I will try to answer your questions the best I can. Let me know if you still have questions:

  1. Keyboard_::begin() and Keyboard_::end() are supposed to start and stop keboard emulation, but they have empty bodies

I believe those are just placeholders in case any initialization or cleanup would need to be done. The other libraries have the same functions (e.g. the Mouse library). I suspect they are there for consistency and just in case they are needed.

  1. KeyReport is especially mysterious.
typedef struct
{
    uint8_t modifiers;
    uint8_t reserved;
    uint8_t keys[6];
} KeyReport;

KeyReport is the data structure that represents the USB message sent to the host computer.

  • The modifiers member is an 8-bit unsigned integer that contains various flags (e.g. Left Shift, Left Ctrl, Left Alt, etc.)
  • The reserved member is an 8-bit unsigned integer that is not used, but must be there.
  • The keys member is an array of six 8-bit unsigned integers that represent the keys that are currently pressed.
  1. What exactly happens to the keyreport? I lost track in USB_Send function in HID.cpp.

It gets sent to the host computer.

  1. What are modifiers, what they are doing?

Some keys are “regular” keys (e.g. A, B, 1, 2, #, etc.). Other keys are modifiers (e.g. Shift, Ctrl, Alt). The modifier keys set flags in KeyReport.modifiers. For example, the Left Shift key is 0x02.

  1. Is number of keys sent limited to 1 or, theoretically could be arbitrary?

The number of “regular” keys that can be press simultaneously is 6, but you could also have the modifier keys pressed (Shift, Alt, Ctrl, etc.).

FYI: I have been able to add additional keys (e.g. the numeric keypad keys) by adding new key definitions to the USBAPI.h file:

#define KEY_NUMPAD_DIVIDE   0xDC
#define KEY_NUMPAD_MULTIPLY 0xDD
#define KEY_NUMPAD_MINUS    0xDE
#define KEY_NUMPAD_PLUS     0xDF
#define KEY_NUMPAD_ENTER    0xE0
#define KEY_NUMPAD_1        0xE1
#define KEY_NUMPAD_2        0xE2
#define KEY_NUMPAD_3        0xE3
#define KEY_NUMPAD_4        0xE4
#define KEY_NUMPAD_5        0xE5
#define KEY_NUMPAD_6        0xE6
#define KEY_NUMPAD_7        0xE7
#define KEY_NUMPAD_8        0xE8
#define KEY_NUMPAD_9        0xE9
#define KEY_NUMPAD_0        0xEA
#define KEY_NUMPAD_DEL      0xEB

Upvotes: 1

Related Questions