Macinkeys
Volume Number: 4
Issue Number: 12
Column Tag: Pascal Procedµres
Inside Macinkeys
By Fabien Samuel, Paris France
Fabien Samuel is a mathematics teacher in Paris, as well as a convinced
Macintosh adept and Programmer.
Getting Info
This article is meant to give a comprehensive overview about the various
resources, structures and procedures involved in keyboard events, and e specially
about the relation between Keycodes and Ascii Codes. There has already been a couple of
very good articles on that subject in MacTutor, and I must thank Joel West and Jörg
Langowski for the help I found in these as a starting point. The present article tries to
update and complete the information with respect to the new resources in System 4.0
or later.
As an example, I have included the KeyEdDemo application, which allows you to
remap your keyboard according to specific needs and save that configuration both in
RAM, for immediate access, and in your system file, so as to keep the changes after
reboot. This is a very simplified version of my shareware application KeyEd 0.5, that
features a fully functional file menu, multiple scrolling windows, extensive font and
view options, and advanced error checking. The main purpose of this demo version is to
show how the resources described below can be used.
The information presented here has been obtained by various means. Lacking
documentation about the new resources introduced with system 4.0, the most valuable
help was found in MacNosy. I had quite the feeling of being a detective, looking through
the nosied code of INIT 0 and _KeyTrans to deduce the structure of KCHR resources.
When I finally got Inside Mac Vol V, I was able to confirm some of my discoveries
and find some more information, although the information presented there is not
always very reliable.
Transitions and Codes
The keyboard contains its own micro-processor, which returns to the Mac OS
Transitions Codes, also known as Raw Key Codes, every time the user types a key.
Those Raw Key Codes are then translated in Virtual Key Codes, involving the use of a
translation table contained in KMAP resources. The details of this translation are
beyond the scope of this article, and in everything that follows we will call Key Codes
the virtual key codes that were so obtained.
Key Codes can take any value between 0 and 127, not all values being actually
used (at least until they release a super-extended keyboard with 128 keys!). A 16
bytes long variable, found in the low-memory global KeyMap ($174), tells which
keys are down at any given time: each set bit indicating that the corresponding key is
down. At least 2 normal keys can be thus indicated, along with any combination of the
modifiers keys (Shift, option, etc...).
Figure 1: Relation between KeyMap and Key Codes. KeyCaps from the Extended US ADB
Keyboard have been shown along with the hex key codes , and the bit offset information
.