Never heard of modifier keys? Maybe you didn’t, but for sure you are using them every single day. Keys such as the Shift, Ctrl and Alt are technically called modifier keys. What I surprisingly learned about them, read below…
I was recently building a button box to have a physical device to control a video game. That button box just simply contains (as the name implied) a lot of buttons, hooked up to a microcontroller. The antique atMega8 Microcontroller is running a firmware with embedded V-USB to simulate regular USB keyboard.
Once the project was in an advance state I though about implementing “another layer” for me buttons and thought about simply using one of the existing keyboard modifier keys. As mentioned above, modifier keys are Shift, Ctrl, Alt and Cmd (or the Windows logo). Pressing a regular character key on you keyboard while holding created a different symbol on your screen. In most of the cases, the character is just in upper case, but number will for example turn into special characters.
How does that exactly work? Does the keyboard send a different key code to the computer which is then checking which character to print?
I found a great website, which describes the Keyboard Report Format, and it is quite simple: A message from the USB keyboard to the computer is exactly 9 bytes long.
Let’s focus on the second byte and the last six bytes. While the last six bytes contain up to six key codes, the second byte contains the modifier mask. For example, if you hold the Shift key while pressing another key, the keyboard is creating a report for the computer which consists of the key code in fourth byte and a 0x01 in the second byte which stands for “Left Shift key pressed”.
The computer is then combining the key code with the modifier mask and determines the character to be displayed.
However, why am I so curious about that? Well I did some testing on different operating systems and I noticed the following.
If you hook up two USB keyboards to your machine and you press the Shift key on keyboard no. 1 and a random character key on keyboard no. 2, what would you expect to happen with the modifier mask?
Actually not answered easily. I found out, that on Linux (Ubuntu) and Windows, the modifier keys are registered across keyboards. In my example from above, the random pressed character key would be in upper case.
Apart from that, my Mac seems to respond differently, the Shift key from the “other” keyboard is ignored, as long as I type on “another” keyboard. Strange fact, unlikely, that you need this information at some time later in your life, but maybe that’s interesting for someone else than me as well… 🙂