4 years, 9 months ago.

USB Keyboard, 3 key combinations

Hello

I'm using MBed board to forward key strokes and mouse movements from a control PC to a target PC. I already have the system working for single keystrokes and two key combinations with a modifier key, such as CTRL + C. However I have been unable to figure out how I can send key combinations with 3 keys, such as CTRL + ALT + DEL or CTRL + SHIFT + ESC.

I'm using Mbed-os 5.12.0 and the included USBMouseKeyboard library. With single keystrokes and combinations with a modifier key I have so far survived with keyboard.printf() and keyboard.key_code() as shown in examples.

So how can I get 3 key combinations with two modifier keys to work?

EDIT: I just managed to find a solution based on https://usb.org/sites/default/files/documents/hid1_11.pdf page 66/97. The example shows that the modifier keys are all sent in one modifier byte. In the example CTLR + ALT results in modifier byte value of 00010100, which equals 0x14. This means we can do keyboard.key_code(KEY_DELETE, 0x14); to send CTRL+ALT+DEL key combination.

Combinations of any other modifier keys can be used by figuring out the correct modifier byte value using the Bit and Key table on the same page.

1 Answer

4 years, 8 months ago.

The full list of keyboard modifiers are:

enum MODIFIER_KEY {
    KEY_CTRL = 0x01,
    KEY_SHIFT = 0x02,
    KEY_ALT = 0x04,
    KEY_LOGO = 0x08,
    KEY_RCTRL = 0x10,
    KEY_RSHIFT = 0x20,
    KEY_RALT = 0x40,
    KEY_RLOGO = 0x80,
};

So CTRL-ALT-Delete could be written as keyboard.key_code(KEY_DELETE,KEY_CTRL | KEY_ALT ); For software that cares about exact key use your use of 0x14 is using the left hand ALT key and the right hand CTRL key.

Accepted Answer

Thank you. That is actually better way to write it than my own solution. Keeps the code more readable and don't have to go working out the hex values for each key combination.

posted by Aslak K 29 Jul 2019