10 years, 5 months ago.

USBKeyboard and multiple modifiers

Hello all, I'm using an LPC1768 as a method for sending key strokes triggered via Ethernet (for a testing a hardware device).

So far I have the sockets all setup fine. The trouble I'm having is with sending CTRL+ALT+m

The pertinent line is:

my_keyboard.keyCode('m', KEY_CTRL|KEY_ALT);

I believe the use of the | allows a second modifier key. I'm sure I've had this working in the past but I can no longer find the documentation that says to use a pipe for an 'and' in this case.

Is this the correct approach?

1 Answer

10 years, 5 months ago.

In c the | operator indicates a bitwise or (as opposed to || which is a logical or) and is often used to create a bitfield of multiple flags.

In terms of CPU cycles this is a very efficient way to allow any permutation of the possible options to be passed without all the mess of having to cope with varying lengths of parameters being passed to functions.

KEY_CTRL is defined as being 1, KEY_SHIFT = 2 and KEY_ALT = 4,

Which means that KEY_CTRL | KEY_ALT means 0x01 ored with 0x04 = 0x05

Due to the way the values are defined you could add them together and get the same result but that could take more CPU cycles.

Accepted Answer