Wed Aug 2 23:14:53 CEST 2006

Carbon keyboard events

One items on Mac porting list of things to do is keyboard support. For this, we need to understand which keyboard events you can receive in Carbon. All keyboard events are of class kEventClassKeyboard (see Carbon Event Manager Reference), section Keyboard events.

Four events are interesting for us:
  • kEventRawKeyDown - some key was pressed down
  • kEventRawKeyUp - some key was released
  • kEventRawKeyRepeat - some key was pressed in the past and still held down, so Repeat event is sent instead of Down event
  • kEventRawKeyModifiersChanged - the status of some (or even more) keyboard modifiers changed
Keyboard events can have one of the following parameters (not all parameters are described here):
  • kEventParamKeyModifiers (typeUInt32) - contains information about status of several modifier keys (see below). This parameter is present with every event mentioned above
  • kEventParamKeyCode (typeUInt32) - see my previous blog about keycodes ;-) Almost every key on the keyboard has its own keycode. We will probably not use it for all keys, only for some (see below for reasons.)
  • kEventParamKeyMacCharCodes (typeChar) - contains the character that this key generates.
Parameters kEventParamKeyCode and kEventParamKeyMacCharCodes are only present for Up, Down and Repeat events (not for ModifiersChanged event). Notice that Repeat event doesn't have "repeat count"!

Keyboard modifiers bits are: cmdKeyBit (8), shiftKeyBit (9), alphaLockBit (10), optionKeyBit (11), controlKeyBit (12), rightShiftKeyBit (13), rightOptionKeyBit (14), rightControlKeyBit (15), kEventKeyModifierNumLockBit (16), kEventKeyModifierFnBit (17). My keyboard is not able to distinguish left and right keys though ("left" bit is set instead).

Sample patch for keyboard events handling is here. Cursor keys up, down work (even with repeat now). Page Up and Page Down work as well. In the font dialog, try to enter some font names or variants or even sizes. All should work now (at least it works here ;-). There are minor issues (matching the current state) - when you select the complete contents of input box, it is not highlighted etc.

Posted by Pavel | Permanent link | File under:, Mac OS X