TweetFollow Us on Twitter

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 especially 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 .

The contents of this variable can also be obtained through the Pascal procedure GetKeys (TheKeys: KeyMap). A few important notes must be made here. First of all, although the standard interface for Type KeyMap is Packed Array [0..127] of Boolean, a limitation of the LightSpeed Pascal compiler redefines it as Array [0..3] of LongInt. The underlying structure remains the same, but it cannot be accessed in the same manner. Second, as shown in Figure 1, KeyCodes do not directly correspond to a bit offset in the KeyMap variable, the order of bits being reversed in each byte. This can be very convenient at the assembly language level, where you just need to shift the KeyCode by 3 bits to get the byte offset, the remainder being the bit offset within that byte that can be used along with instructions like BTST.

Things get more complicated in Pascal if one wants to use the bit-wise ToolBox utilities, as bits are computed in the reverse order (Higher to Lower bits, Lower to Higher Bytes) than they are in the corresponding assembly instructions. However the following code shows a way to access a KeyMap that will work with any compiler:

Var TheKeys:KeyMap; KeyCode,Offset:Integer;
Offset:= KeyCode + 7 - 2 * (KeyCode mod 8);
if BitTst(@TheKeys,Offset) then { Key is down }

Different KeyBoards

According to the countries and Mac models, the relation between physical keys and their key codes can vary greatly. This can be due to distinct physical mappings, for instance the return key or the cursor keys can be found at different locations from one keyboard to another. To make things even simpler, keys having the same function can have different key codes, or, better still, keys having the same keycodes can have different meanings! We will try to summarize these exceptions in a moment.

There currently are 6 different Apple keyboards, each one having its own keyboard type (Found in low-memory global KybdType = $21E - Byte value). However the keyboard types for the US classic and Euro classic keyboards are both equal to 3, despite their important differences: a way to distinguish between them is shown in the demo program (Function GetKbd) . A display map of these keyboards can be found in KCAP resources, contained in the system file “Key Layout”, that we will discuss later.

In chronological order, the Mac Classic (KybdType = 3) keyboards exist in US Version (KCAP ID = 3) and International Version (KCAP ID = 259). For archaeologists that might be interested, those are the keyboards that were released with the antiques 128 and 512 Macs. Then came the Mac Plus keyboard (KybdType = KCAP ID = 11), and the 3 ADB Keyboards released with the SE and Mac II: Standard (ID = 1), Extended (ID = 2) and ISO (ID = 4), this last one satisfying with the international ISO specifications, unlike the Standard ADB Keyboard.You can find representations of all those keyboards in Figures 2 to 7, along with the corresponding key codes.

Figure 2.

Figure 3

Figure 4.

Figure 5.

Figure 6.

Figure 7

We can now see better what are the main differences between keyboards:

1. Return and \: On the US Classic Keyboard the keycode for Return is $24, whereas on the Euro Classic it is $2A (which maps to the \ character on the US Classic). In all other keyboards, the keycode for Return is still $24, and in all cases the ascii code is and should be $0D.

2. Enter and Space: again the corresponding keycodes ($34 for Enter and $31 for Space on the MacClassic) have been inverted on the EuroClassic keyboard. On all subsequent keyboards, the Enter key has been moved to the numeric keypad, with keycode $4C, the Space key keeping its $31 keycode.

3. Cursor Keys: originally, those keys were only available on the optional numeric keypad, where they also served as operator keys when shifted. With the introduction of the Mac Plus built-in numeric keypad, the cursor keys were moved to the main keyboard, while the numeric operator keys stayed where they belonged on the keypad. The only problem is that they kept the same keycodes! To solve this conflict, when you type one of the numeric operator keys on the Plus, it acts as if you had held the shift key down (and likewise, holding down the shift key while typing one of the cursor keys will return the corresponding numeric operator). On subsequent models, this conflict was resolved by giving altogether different keycodes to both the numeric operator and cursor keys: this means 8 new key codes have been introduced for that purpose. The situation is summarized in Figure 8. Also note that the + and - keys on the keypad have been inverted between the ADB extended and standard keyboards.

The main conclusion of all this is that you should only rely on the ascii code of such control characters, which always keeps the same value whatever keyboard you use.

Figure 8.

Keyboard Events

For most purposes, dealing with keyboard input mainly involves polling KeyDown and AutoKey events. Information about such an event is given in the Message and Modifiers fields of the EventRecord returned by GetNextEvent. The Message field has the following 4 bytes long structure:

<Reserved, ADBAddress, keycode, ascii code>

ADBAddress is used with the Mac SE and II, and contains a reference number of the keyboard the typing came from, useful if various keyboards are simultaneously connected (see January 88 MacTutor for an example of this). Ascii code is obtained through the conversion routines that are discussed in the next paragraphs, taking into account both the key code and the state of modifiers keys.

The Modifiers field of the EventRecord is made up of 2 bytes, the high byte giving the state of the modifiers keys in the following bit-arrangement:

<x,y,z,control,option,caps lock,shift,command>

Flags x,y and z are normally zero. However, the extended ADB keyboard can be reconfigured so as to distinguish between the left and right Shift,Option and Control Keys. In that case, the right keys will generate new key codes, and flags x,y,z could be renamed: R.Control,R.Option,R.Shift. This possibility is supported in order to use the Mac with an operating system Inside Mac doesn’t dare to mention, so neither will I! It can be implemented through the use of the ADB operators mechanism, which might be the subject of another article. Anyway, this capacity is strongly discouraged because it doesn’t comply with the User Interface.

Finally, let’s note that some keys do not immediately return an event: those are the keys that are used in conjunction with another key to produce exotic objects like accented characters. I will refer to them as “Double Strike Keys”, also the official terminology appears to be “Dead Keys”, but I just don’t like the idea of my keys being dead!

Figure 9.

In the beginning, there was INIT 0 and 1...

On the first Macs, and until the Mac Plus with systems 3.x, all the information related to converting key codes to ascii codes was contained in system resources INIT 0 and 1. The Localizer was in charge of adapting these resources according to the various countries. Those resources contained both the conversion code and the associated translation tables.

With systems 4.x and later, The INIT’s now mainly contain code, the translation tables being now separate and contained in the KCHR 0 system resource. At boot time, the INIT’s are loaded into the system heap and jumped to. They are mainly in charge of installing pointers to the conversions routines in low-memory globals Key1Trans and Key2Trans.

The reason for there being two separate routines is due to the fact that originally, the numeric keypad was optional. Key1Trans will thus deal with keycodes from 0 to 63, while Key2Trans treats keycodes from 64 to 127, that used to be found exclusively on the numeric keypad. This distinction now appears completely artificial (if not intelligent...) since the Mac Plus was introduced. It seems that those routines have only stayed for compatibility reasons, as their codes are now almost the same, and both refer to the new _KeyTrans ToolBox trap, which does most of the work.

The Inner Workings

To start with, I will describe the main steps involved in INIT 0, INIT 1 having a similar functionality. As any INIT resource, it gets executed at boot time after being loaded as a locked relocatable block, accessed through a handle. It’s made up of two essential routines that I’ll call SetUp and Key1TransMain. SetUp is the part that gets jumped to at boot time and does the following :

1. Load KCHR 0 into the system heap, if it’s not already there.

2. Create a non-relocatable copy of KCHR 0 in system heap and store a pointer to it at offset 14 in a parameter block that is accessed through the pointer maintained in BasicGlob (= $2B6) low-memory global. Actually, this copy is expanded by at least 512 bytes, probably in order to allow its modification by another program.

3. Copy Byte 6 from the itlc 0 in a Data zone internal to INIT 0. This is an international utilities resource, and the value of this byte will be used to distinguish between the US Classic and Euro Classic keyboards, as illustrated in the GetKbd function of the demo program. This actually must mean that running with an Euro Classic keyboard and a US system would make believe the keyboard was US [This is true, recalling all the mixups we had using different systems - JL]: there doesn’t seem to be a hardware method to distinguish between the two keyboards.

4. Put the entry point to Key1TransMain in low-memory global Key1Trans.

5. Cut down the size of its own handle before returning, the SetUp routine being now useless (this is accomplished by putting those last instructions at the beginning of the block, unless one wants to call this the Auto-Destroy routine!).


Key1transMain is the part currently used every time a keydown or keyup event is detected by the Event Manager. This is a register-based routine, and calling it from an high level language involves assembler glue-code (see the Demo Program). It takes on entry:

D2 contains the keycode (less than 64, or nothing will happen: in that case one must call Key2Trans)

D1 contains the state of the modifiers keys (as it appears in the third word of the KeyMap structure: this differs from the EventRecord Modifiers field).

D3 should be 0 for most purposes: bit 7 can be set to indicate that the event should be treated as a key-up event rather than a key-down event.

On exit, D0 contains the resulting ascii code.

This routine works as follows:

1. Make up a word that has the following structure:

- the high byte contains the modifier flags as they would appear in the EventRecord modifiers field:


Note that the command key state is read directly on the keyboard when the routine is executed, and so doesn’t depend on the one you pass in D1.

- the low byte contains the key code, with a flag in bit 7 indicating KeyUp status.

2. Call the ToolBox function

KeyTrans (transData:Ptr; KeyCode:Integer; VAR State: LongInt): Longint;

where KeyCode is the word obtained in step 1, TransData a pointer to the copy of KCHR 0 accessed through BasicGlob as described above, and State an offset used for double strike keys that is maintained at offset 10 in the parameter block pointed to by BasicGlob. We will see how this works in just a moment .

The function result contains in its Low Word the ascii code of the current key, taking into account its possible combination with a previous double strike key. The High Word returns the ascii code for an eventually waiting double strike if it couldn’t be combined with the current key.

3. In case the High Word is non-zero, Key1Trans will post an event for it, thus causing the default ascii value of the previous double strike to be reported before that of the current key.

4. In any case, the ascii code for the current key and modifiers is returned in D0.

KeyTrans and KCHR 0

What finally remains to complete this overview is to understand how the KeyTrans function is working. In order to do that, we first need to know the structure of the KCHR 0 resource, which will incidentally give us the means to modify it.

As I said, KCHR 0 is the fundamental resource that allows to translate a keycode, along with the associated modifiers, to a standard ascii code.

1. The first word of a KCHR resource presently contains zero, and doesn’t seem to be currently used.

2. The next 256 bytes serve as a kind of offset table, each one containing a block number used as follows: the high byte of the Event Record modifiers field can theoretically take any value between 0 and 255, assuming the right modifiers keys are activated. This value serves as an offset into the table to get the corresponding block number. Each block is 128 bytes long, and contains the mapping for every key code when typed along with the above modifiers.

3. The following word contains the actual number of blocks that are used (currently 8, numbered from 0 to 7). This means different modifiers combinations can lead to the same block.

4. Then come the block themselves, each of its 128 bytes giving the ascii code for the corresponding key code, which serves as an offset into the block. If the ascii code is zero, it means it’s either a double strike key, a modifier key, or no key at all.

5. Finally, we find a variable length part that deals with double strikes.

a) The first word contains the total number of double strike keys.

b) For each one of them, we find a structure organized as follows:

b1) 2 bytes, giving the block number and key code of that double strike.

b2) Word giving the total number of associated variants.

b3) List of all those variants in the form: Current Ascii Code / Resulting Ascii Code

b4) Word giving the default ascii value of that double strike, in case the current ascii code wouldn’t combine with it to produce a resulting ascii code.

Let’s give an example to try and make things a bit clearer: on the US keyboard, Option E followed by E results in the accented Character é, but if followed by P it will result in the two consecutive characters ´p. The keycode for E is $E, while the option key gives a value of 8 to the modifiers flags high byte. Counting from zero, byte number 8 in the block numbers table has a value of 3, which leads us to block number 3. Looking at byte number $E in that block, we see that its value is zero! This confirms us in thinking that Option E is a double strike. Now, let’s look at the list of double strike keys: the first of them is precisely the one we want: Block Number = 3, KeyCode = $E. The next word tells us 7 variants are associated with it, and among them we find the variant $65,$8F: ascii code $65 (Character e) thus combines with our double strike to produce ascii code $8F (Character é). On the other hand, no variant is associated with ascii code $70 (Character p), and it will thus be the default value $60 (´ accent) that will be returned, followed by the character p.

We finally can explain how the KeyTrans function does its trick. The Modifiers byte is used as an offset into the block numbers table, while the keycode byte serves as an offset into the designated block. If the ascii code it finds at the designated position is non-zero, KeyTrans just returns with it. If it is zero, KeyTrans looks into the double-strike table and tries to match the BlockNumber/Keycode combination with one in the list. If it succeeds, the State variable is loaded with the byte offset between the start of the first block and the location of the “number of variants” entry associated with that double strike. In all other cases, State will be cleared. When KeyTrans is called again, and State is non-zero, it assumes there’s a pending double strike and, after getting the current ascii code, it will look right away at the designated offset to find a match between the current ascii code and an eventual resulting ascii code. If it finds one, it returns with the resulting ascii code. Otherwise it returns with the current ascii code in its low word, and the default ascii code of the pending double strike in its high word. In the last case, and if it has been called by Key1Trans, Key1Trans will post the appropriate event as described above. We can thus see how important it is to maintain the value of State between calls to KeyTrans if we want to get double strikes responses.

As a final point, note that KeyTrans actually does some pre-processing related to the Script Manager.

The Demo Program

The following LightSpeed Pascal program tries to give a simple example of how all this stuff can be put to use. It needs system 4.0 or later to run. It only allows editing of normal keys, and I left out all memory and resources error checking in order to keep it as short as possible. Note that the Shareware version KeyEd 0.5 does implement advanced error-checking features. I’ll leave it as an exercise to the reader to check for Nil Handles and call MemError or ResError when appropriate. However, I couldn’t resist showing how the KCAP’s resources mentioned above can be used in giving a graphical interface to keyboard editing, and that accounts for a large part of the program code. So I first have to say a word about their structure (obtained once again by nosying the KeyCaps DA):

1. Window Rectangle

2. KeyCaps DA TextEdit view rectangle

3. Number of different key shapes

4. For each shape:

a) Number of points used to define it, minus 1.

b) List of those points.

c) Number of associated keys, minus 1.

d) List of those keys in the form:

 <KeyCode,Vertical Offset, Horizontal Offset>.

The points define a region made up of rectangles, that will then be offset to the adequate position for each consecutive key. Note that the keycode field actually contains additional information, so I take it modulo 128. The procedure MainCaps that runs through this structure shows you type-casting in Pascal can do wonders, but would evidently be more elegant in assembly! The only thing I wasn’t able to do with type-casting was to access a byte at a time in memory, so I wrote the short assembly routines Peek and Poke for that purpose, the names being a tribute to Basic programmers.

The program also shows an example of using procedural parameters, a most convenient feature of the LSP compiler. This way the region obtained in MainCaps can be used either for drawing the key or for dealing with a click inside it, or for any other purpose you might want. However, if you want to port that program to another compiler, you’ll need to implement a Case statement within MainCaps to achieve the same result.

Finally, the program also shows the use of a modeless dialog with an User Item. The user procedure UserDraw is attached to that item with the SetUserProc procedure. The Dialog Manager will call it automatically on each update event for the dialog window, and it is in charge of drawing the keyboard. Likewise, if there was a click in that User Item rectangle, we call MainCaps to find if it was in a key, and handle the event accordingly in the ActiveClick procedure. Note that although the Dialog Manager deals with mouse-down events when the Dialog Window is active, you still have to explicitly activate it by calling Select Window when the Event Manager reports a click in the contents region. You also have to pre-process keydown events, to filter out command key equivalents and any other typing you don’t want passed to the edit field. One important warning if you use LightsBug while running this program is that if you inadvertently click in your switched-out Dialog Window, LSP 1.11 will bomb after displaying the usual “You can only drag your program’s windows while it is halted” message. This could be due to the fact that LSP gets notified of the event only after the Dialog Manager has tried to run your User Proc, which will rely on globals that have become invalid since your program is switched out. Anyway, I nearly got my hard-disk trashed while trying to figure out the problem!

Note that the RMaker source file for the program doesn’t include definitions for the KCAP resources used by the program. So after compiling this file with RMaker, you should use ResEdit to copy these resources from the Key Layout file in your system folder and paste them into your resource file. While you’re at it, you could also install BNDL or SIZE resources to make the program look more fancy. It should run at ease under a 50K partition. There are many more possible improvements, but I’ll leave them to your imagination.

Listing:  KeyEdDemo.pas
{ copyright F.Samuel and MacTutor 1988 }
{ use only with system 4.x or later }
 { turn off automatic initialization }
{$L keyEdDemoRes}
 { load resource file }

 DialogID = 128;
 EditCodeItem = 2;
 LoadBtn = 5;
 UserItem = 6;
 AsciiCharItem = 7;
 KeyCodeItem = 8;
 AboutAlrt = 129;
 LoadAlrt = 130;
 AppleID = 1;
 AboutItem = 1;
 FileID = 2;
 EditID = 3;
 CutItem = 3;
 CopyItem = 4;
 PasteItem = 5;
 ClearItem = 6;

{ Low-memory globals }
 KybdType = $21E;
 ScsiFlag = $B22;
 Key1Trans = $29E;
 BasicGlob = $2B6;

{ KCaps ID of various keyboards }
 MacPlusKbd = 11;
 MacClassicKbd = 3;
 EuroMacKbd = 259;
 ADBKbd = 1;
 ADBExtKbd = 2;
 ADBIsoKbd = 4;

 Prect = ^Rect; { for type-casting a pointer }
 PLong = ^LongInt;
 PWord = ^integer;
 PPoint = ^Point;

 Finished, EditOn : Boolean;
 DragRect : Rect;
 MouseLocal : Point;
 KCapsHandle, KChrHandle : Handle;
 DemoDialog : DialogPtr;
 HiliteKeys : SET OF 0..127;
 EditKey, EditModifs : Integer;

{ utilities to acces properties of items in a dialog }

 PROCEDURE SetDItemText (TheDialog : DialogPtr;
 TheItem : Integer;
 TheText : Str255);
 ItemType : integer; { should be a text item }
 ItemHandle : Handle;
 DispRect : Rect;
 GetDItem(TheDialog, TheItem, ItemType, ItemHandle, DispRect);
 SetIText(ItemHandle, TheText)

 FUNCTION GetDItemText (TheDialog : DialogPtr;
 TheItem : Integer) : Str255;
 ItemType : integer; { should be a text item }
 ItemHandle : Handle;
 DispRect : Rect;
 TheText : Str255;
 GetDItem(TheDialog, TheItem, ItemType, ItemHandle, DispRect);
 GetIText(ItemHandle, TheText);
 GetDItemText := TheText

 FUNCTION GetDItemRect (TheDialog : DialogPtr;
 TheItem : Integer) : Rect;
 ItemType : integer;
 ItemHandle : Handle;
 DispRect : Rect;
 GetDItem(TheDialog, TheItem, ItemType, ItemHandle, DispRect);
 GetDItemRect := DispRect

 PROCEDURE SetUserProc (TheDialog : DialogPtr;
 TheItem : Integer;
 TheProc : procPtr);
 ItemType : integer; { should be an UserItem ! }
 ItemHandle : Handle;
 DispRect : Rect;
 GetDItem(TheDialog, TheItem, ItemType, ItemHandle, DispRect);
 SetDItem(TheDialog, TheItem, ItemType, Handle(theProc), DispRect)

{ function NumToString , more convenient that way ... }

 FUNCTION FNumToString (TheNum : LongInt) : Str255;
 TheString : Str255;
 NumToString(TheNum, TheString);
 FNumToString := TheString

{ interface for external procedures and functions : }

 FUNCTION KeyTrans (transData : Ptr;
 keycode : INTEGER;

 PROCEDURE poke (address : longint;
 value : integer);
 external; { puts low byte of value at address }

 FUNCTION peek (address : longint) : integer;
 external; { returns byte at address }

 FUNCTION Key12Trans (KeyCode, KeyModifs : Integer) : Integer;

{ Pascal procedures and functions follow ... }

 FUNCTION GetAscii (KeyCode, Modifs : integer;
 VAR Ascii : integer) : Boolean;
 { Returns true if it’s a normal key , Ascii returns ascii code even 
if it’s a double strike}
 State : LongInt;
 State := 0;
 Ascii := LoWord(KeyTrans(KChrHandle^, KeyCode + Modifs, State));
 IF Ascii = 0 THEN
 GetAscii := false;
 Ascii := LoWord(KeyTrans(KChrHandle^, KeyCode + Modifs, State))
 GetAscii := true

 FUNCTION GetKbd : Integer;
 { returns KCAP ID of current keyboard }
 TempID : integer;
 addr : Plong;
 SCSIMac : boolean;
 TempId := peek(KybdType);
 SCSIMac := BitTst(Ptr(SCSIFlag), 5);
 IF (NOT SCSIMac) AND (TempID <> MacPlusKbd) THEN
 tempId := MacClassicKbd;
 addr := Plong(Key1Trans);
 IF peek(addr^ + 10) <> 0 THEN { test itlc byte }
 TempId := EuroMacKbd
 GetKbd := TempID

 ID : integer;
 FOR ID := AppleID TO EditID DO
 InsertMenu(GetMenu(ID), 0);
 AddResMenu(GetMHandle(AppleID), ‘DRVR’);

 PROCEDURE MainCaps (PROCEDURE treatIt (rgn : RgnHandle;
 Code : integer));
 Hrgn : RgnHandle;
 addr : PWord;
 Paddr : PPoint;
 NumRgn, NumRect, NumKeys, i, j : integer;
 Keycode, dh, dv : Integer;
 TL, BR : point;
 KRect : rect;
 Addr := Pword(Ord4(KcapsHandle^) + 16);
 NumRgn := Addr^;
 IF NumRgn > 0 THEN
 FOR i := 1 TO NumRgn DO
 Addr := Pword(Ord4(Addr) + 2);
 NumRect := addr^;
 Hrgn := NewRgn;
 SetPt(TL, 0, 0);
 Addr := Pword(Ord4(Addr) + 2);
 FOR j := 0 TO NumRect DO
 PAddr := PPoint(addr);
 BR := Paddr^;
 Pt2Rect(TL, BR, Krect);
 TL := BR;
 Addr := Pword(Ord4(Addr) + 4);
 NumKeys := addr^;
 FOR j := 0 TO NumKeys DO
 Addr := Pword(Ord4(Addr) + 2);
 KeyCode := addr^;
 Addr := Pword(Ord4(Addr) + 2);
 dv := addr^;
 Addr := Pword(Ord4(Addr) + 2);
 dh := addr^;
 OffsetRgn(Hrgn, dh, dv);
 TreatIt(Hrgn, Keycode MOD 128);

 PROCEDURE InvertKey (Rgn : RgnHandle);
 InnerRgn : RgnHandle;
 InnerRgn := NewRgn;
 CopyRgn(Rgn, InnerRgn);
 InsetRgn(InnerRgn, 2, 2);

 PROCEDURE DrawKey (rgn : RgnHandle;
 Code : integer);
 DrawRgn : RgnHandle;
 AsciiCode : Integer;
 NormalKey : boolean;
 DrawRgn := NewRgn;
 CopyRgn(Rgn, DrawRgn);
 InsetRgn(DrawRgn, 1, 1);
 NormalKey := GetAscii(Code, EditModifs, AsciiCode);
 WITH DrawRgn^^.rgnBBox DO
 MoveTo(left + 1, bottom - 2);
 IF Code IN HiliteKeys THEN

 PROCEDURE UserDraw (TheWindow : WindowPtr;
 ItemNum : Integer);
 FillPat : Pattern;
 TheRect : Rect;
 TheRect := GetDItemRect(DemoDialog, ItemNum);
 GetIndPattern(FillPat, 0, 10);
 FillRect(TheRect, FillPat);

 PROCEDURE InitThings;
 i, Error : Integer;
 BEGIN { Get KybdID and KCaps ; SetUserProc;Show dialog }
 FlushEvents(EveryEvent, 0);
 SetEventMask(EveryEvent - KeyUpMask);
 FOR i := 1 TO 5 DO
 WITH ScreenBits.Bounds DO
 SetRect(DragRect, left + 4, top + 24, right - 4, bottom - 4);
 KCapsHandle := GetResource(‘KCAP’, GetKbd);
 DetachResource(KCapsHandle); { don’t let a DA release it on your back 
! }
 KChrHandle := GetResource(‘KCHR’, 0);
 Error := HandToHand(KChrHandle); { make a copy , so we can modify it 
 MoveHHi(KChrHandle); { keep heap unfragmented }
 HiliteKeys := []; { no keys to hilite until the user selects one }
 EditModifs := 0;
 EditOn := False; { wait for the user to select a key to edit }
 DemoDialog := GetNewDialog(DialogID, NIL, WindowPtr(-1));
 SetUserProc(DemoDialog, UserItem, @UserDraw);
 Finished := False

 PROCEDURE DoMenu (Code : longint);
 MenuNum, ItemNum, Temp : integer;
 DeskAccName : str255;
 IF code <> 0 THEN
 MenuNum := HiWord(Code);
 ItemNum := LoWord(Code);
 CASE MenuNum OF
 AppleID : 
 IF ItemNum = AboutItem THEN
 Temp := Alert(AboutAlrt, NIL)
 GetItem(GetMHandle(AppleID), ItemNum, DeskAccName);
 Temp := OpenDeskAcc(DeskAccName);
 FileID : 
 Finished := True;
 EditID : 
 IF NOT SystemEdit(ItemNum - 1) THEN
 IF (FrontWindow = DemoDialog) AND EditOn THEN
 CASE ItemNum OF
 CutItem : 
 CopyItem : 
 PasteItem : 
 ClearItem : 

 PROCEDURE StartEdit (KeyCode, AsciiCode : Integer);
 { enable editing  the ascii code of the selected key }
 EditKey := KeyCode;
 HiliteKeys := HiliteKeys + [EditKey];
 SetDItemText(DemoDialog, EditCodeItem, FNumToString(AsciiCode));
 SelIText(DemoDialog, EditCodeItem, 0, MaxInt);
 SetDItemText(DemoDialog, KeyCodeItem, FNumToString(EditKey));
 SetDItemText(DemoDialog, AsciiCharItem, Chr(AsciiCode));
 EditOn := true

 PROCEDURE DrawEditKRgn (Rgn : RgnHandle;
 TheCode : Integer);
 { update key contents in case it changed }
 IF TheCode = EditKey THEN
 DrawKey(Rgn, EditKey)

 PROCEDURE ValidateEdit;
 { validate user editing of the selected key }
 BlockNumber : Integer;
 MapAddress, NewAsciiCode : LongInt;
 StringToNum(GetDItemText(DemoDialog, EditCodeItem), NewAsciiCode);
 MapAddress := Ord4(KChrHandle^);
 BlockNumber := Peek(MapAddress + BitShift(EditModifs, -8) + 2);
 Poke(MapAddress + 260 + BlockNumber * 128 + EditKey, NewAsciiCode);
 EditOn := False;
 SetDItemText(DemoDialog, EditCodeItem, ‘’);
 SetDItemText(DemoDialog, KeyCodeItem, ‘’);
 SetDItemText(DemoDialog, AsciiCharItem, ‘’);
 HiliteKeys := HiliteKeys - [EditKey];

 PROCEDURE CodeXORModifs (ModifCode : Integer;
 VAR Modifs : Integer);
 { XOR new modifier with the already selected ones }
 TempModifs : Integer;
 TempModifs := 0;
 BitSet(@TempModifs, $3E - ModifCode);
 Modifs := BitXor(Modifs, TempModifs)

 FUNCTION EditPerm (KeyCode, Modifs : Integer;
 VAR AsciiCode : Integer) : Boolean;
{ can that key  be edited ? }
 IF GetAscii(KeyCode, Modifs, AsciiCode) THEN
 EditPerm := true
 ELSE { check that it’s not a double strike nor a modifier }
 EditPerm := (AsciiCode = 0) AND NOT (KeyCode IN [$3C..$3E])

 PROCEDURE ActiveClick (rgn : RgnHandle;
 Code : integer);
 AsciiCode : Integer;
 IF PtInRgn(MouseLocal, Rgn) THEN
 IF (Code <> EditKey) OR NOT EditOn THEN
 IF Code IN [$37..$3B] THEN { modifier key was clicked }
 CodeXORModifs(Code, EditModifs);
 IF Code IN HiliteKeys THEN
 HiliteKeys := HiliteKeys - [Code]
 HiliteKeys := HiliteKeys + [Code];
 IF EditPerm(EditKey, EditModifs, AsciiCode) THEN
 StartEdit(EditKey, AsciiCode);
 MainCaps(DrawKey)  { redraw the keyboard to update hiliting }
 ELSE IF EditPerm(Code, EditModifs, AsciiCode) THEN
 InvertKey(Rgn); { hilite it , and let user edit it }
 StartEdit(Code, AsciiCode)

 FUNCTION CheckSysTrans (TransData : Ptr) : Boolean;
 { is TransData really a pointer to the system mapping table ?? }
 KeyCode, AsciiCode : Integer;
 CheckSysTrans := true;
 FOR KeyCode := 0 TO 16 DO
 AsciiCode := Key12Trans(KeyCode, 0); { let system compute it }
 IF Peek(Ord4(TransData) + 260 + KeyCode) <> AsciiCode THEN
 CheckSysTrans := False; { compare with our table }
 IF AsciiCode = 0 THEN
 AsciiCode := Key12Trans(KeyCode, 0);
 FlushEvents(EveryEvent, 0) { double strike may have posted an event 
, flush it }

 FUNCTION GetSysTrans (VAR TheTrans : Ptr) : boolean;
 BGlob, Addr : PLong;
 Bglob := PLong(BasicGlob);
 Addr := PLong(BGlob^ + 14);
 { not documented , so better check if we can rely on it ! }
 TheTrans := Ptr(Addr^);
 GetSysTrans := CheckSysTrans(TheTrans)

 TheSize : LongInt;
 RamTransPtr : Ptr;
 SysKchr : Handle;
 AppResFile : integer;
 IF Alert(LoadAlrt, NIL) = Ok THEN
 TheSize := GetHandleSize(KChrHandle);
 IF GetSysTrans(RamTransPtr) THEN { see above ... }
 BlockMove(KChrHandle^, RamTransPtr, TheSize);
 AppResFile := CurResFile;
 SysKchr := GetResource(‘KCHR’, 0);
 BlockMove(KChrHandle^, SysKChr^, TheSize);

 PROCEDURE DoDialog (TheEvent : EventRecord);
 Enter = $03;
 Return = $0D;
 TheDialog : DialogPtr;
 ItemHit, CharCode : Integer;
 PassIt : Boolean;
 WITH TheEvent DO
 IF What = KeyDown THEN
 BEGIN { filter key down events }
 CharCode := BitAnd(Message, CharCodeMask);
 PassIt := False;
 IF BitAnd(Modifiers, CmdKey) <> 0 THEN
 IF CharCode IN [Return, Enter] THEN
 PassIt := True
 PassIt := True;
 IF DialogSelect(TheEvent, TheDialog, ItemHit) THEN
 CASE ItemHit OF
 UserItem : 
 LoadBtn : 

 GotEvent : Boolean;
 TheEvent : EventRecord;
 TheWindow : WindowPtr;
 GotEvent := GetNextEvent(EveryEvent, TheEvent);
 IF IsDialogEvent(TheEvent) THEN
 WITH TheEvent DO
 MouseDown : 
 CASE FindWindow(Where, TheWindow) OF
 inMenuBar : 
 inSysWindow : 
 SystemClick(TheEvent, TheWindow);
 inContent : 
 IF TheWindow <> FrontWindow THEN
 inDrag : 
 DragWindow(TheWindow, Where, DragRect);
 inGoaway : 
 IF TheWindow <> FrontWindow THEN
 ELSE IF TrackGoAway(TheWindow, Where) THEN
 Finished := True;
 KeyDown : 
 IF BitAnd(Modifiers, CmdKey) <> 0 THEN
 DoMenu(MenuKey(Chr(BitAnd(Message, CharCodeMask))));
 UpdateEvt : 
 BEGIN { just in case ... }
 TheWindow := WindowPtr(Message);

 UNTIL Finished
Listing:  KeyEdDemo.asm

;MDS source code for KeyEDDemo External procedures
;copyright F.Samuel and Mac Tutor 1988
; After compiling , turn the .rel file into a LSP
; library using rel.Converter , then include the lib
; into your LSP project 

;MDS Glues for Pascal Peek and Poke
;procedure poke(address:ptr;value:byte)
;function peek(address:ptr): byte;

XDEF peek
XDEF poke

value EQU 8
addr1 EQU 10

 LINK A6,#0
 MOVE value(A6),D0
 MOVE.B D0,(A0)
 JMP  (A0)
addr2 EQU 8
result  EQU 12
 LINK A6,#0
 MOVEQ  #0,D0
 MOVE.B (A0),D0
 MOVE D0,result(A6)
 JMP  (A0)
; Function Key12Trans(KeyCode,KeyModifs:Integer) : Integer;
; KeyModifs are as they appear in the third word of KeyMap
; Function returns AsciiCode

XDEF    Key12Trans

Key1Trans EQU  $29E
Key2Trans EQU  $2A2

KeyModifs EQU  8
KeyCode EQU 10
FunRslt EQU 12


 LINK A6,#0
 MOVE KeyCode(A6),D2
 MOVE KeyModifs(A6),D1
 MOVEQ  #0,D3
 CMPI #64,D2
 BHS.S  @1
 BRA.S  @2
@1 MOVEA.LKey2Trans,A0
@2 JSR  (A0)
 MOVE D0,FunRslt(A6)
 ADDQ.L #4,SP 
 JMP  (A0)
Listing:  KeyEdDemoRes.R
* KeyEdDemo resource definitions for RMaker *
* Copyright 1988 - F.Samuel and Mac Tutor   *
* After compiling , don’t forget to add the *
* KCAP resources from Key Layout in your    *
* System Folder , using ResEdit  . Then          *
* include the file into your LSP project    *
* using Run Options from the Project Menu   *



About KeyEdDemo


Paste /V


Key Ed Demo
76 14 306 504
Invisible GoAway
4;definition ID
0;ref con


42 102 94 410
CCCC  ;Alert stages

42 136 142 376


*   1
StatText Disabled
5 3 25 89
Ascii Code : 

*   2
EditText Disabled
5 96 25 133

*   3
StatText Disabled
5 279 25 357
Key Code : 

*   4
StatText Disabled
5 144 25 221
Ascii Char : 

*   5
BtnItem Enabled
5 418 25 478

*   6
UserItem Enabled
32 5 222 482

*   7
StatText Disabled
5 223 25 257

*   8
StatText Disabled
5 360 25 393

*   1
StatText Enabled
5 3 48 305
Key Ed Demo\0DCopyright Fabien Samuel and Mac Tutor 1988

*   1
BtnItem Enabled
75 6 95 66

*   2
BtnItem Enabled
75 170 95 230

*   3
StatText Disabled
6 5 65 232
Are you sure you want to load this mapping into the System File and into 
RAM ? 

Community Search:
MacTech Search:

Software Updates via MacUpdate

TunnelBear 3.0.1 - Subscription-based pr...
TunnelBear is a subscription-based virtual private network (VPN) service and companion app, enabling you to browse the internet privately and securely. Features Browse privately - Secure your data... Read more
ExpanDrive 5.4.0 - Access cloud storage...
ExpanDrive builds cloud storage in every application, acts just like a USB drive plugged into your Mac. With ExpanDrive, you can securely access any remote file server directly from the Finder or... Read more
Opera 40.0.2308.62 - High-performance We...
Opera is a fast and secure browser trusted by millions of users. With the intuitive interface, Speed Dial and visual bookmarks for organizing favorite sites, news feature with fresh, relevant content... Read more
Apple iOS 10.0.2 - The latest version of...
iOS 10 is the biggest release of iOS ever. A massive update to Messages brings the power of the App Store to your conversations and makes messaging more personal than ever. Find your route with... Read more
Herald 7.0 - Notification plugin for Mai...
Note: Versions 2.1.3 (for OS X 10.7), 3.0.6 (for OS X 10.8), 4.0.8 (for OS X 10.9), 5.0.2 (for OS X 10.10), and 6.0.3 (for OS X 10.11) are no longer supported by the developer. Herald is a... Read more
Postbox 5.0.2 - Powerful and flexible em...
Postbox is a new email application that helps you organize your work life and get stuff done. It has all the elegance and simplicity of Apple Mail, but with more power and flexibility to manage even... Read more
Yummy FTP Pro 1.11.10 - $29.99
Yummy FTP Pro is an advanced Mac file transfer app which provides a full-featured professional toolkit combined with blazing speeds and impeccable reliability, so whether you want to transfer a few... Read more
Tidy Up 4.1.17 - $29.99
Tidy Up is a full-featured duplicate finder and disk-tidiness utility. Features With Tidy Up you can search for duplicate files and packages by the owner application, content, type, creator,... Read more
Logitech Control Center 3.9.5 - Keyboard...
Logitech Control Center (LCC) is designed to support OS X and allows you to take full advantage of your Logitech keyboard, mouse, or trackball. With the LCC you can: Browse the Internet using... Read more
Macgo Blu-ray Player 2.16.17 - Blu-ray p...
Macgo Blu-ray Player can bring you the most unforgettable Blu-ray experience on your Mac. Overview Macgo Mac Blu-ray Player can satisfy just about every need you could possibly have in a Blu-ray... Read more

How to build and care for your team in D...
Before you hit the trail and become a dog sledding legend, there’s actually a fair bit of prep work to be done. In Dog Sled Saga, you’re not only racing, you’re also building and caring for a team of furry friends. There’s a lot to consider—... | Read more »
How to win every race in Dog Sled Saga
If I had to guess, I’d say Dog Sled Saga is the most adorable racing game on the App Store right now. It’s a dog sled racing sim full of adorable, loyal puppies. Just look at those fluffy little tails wagging. Behind that cute, pixelated facade is... | Read more »
Let the war games commence in Gunship Ba...
Buzz Lightyear famously said, “This isn’t flying, this is falling – with style!” In the case of Gunship Battle: Second War, though, this really is flying - with style! The flight simulator app from Joycity puts you in control of 20 faithfully... | Read more »
How to get a high score in Fired Up
Fired Up is Noodlecake Games’ high score chasing, firefighting adventure. You take control of a wayward firefighter who propels himself up the side of a highrise with blasts of water. Sound silly? It is. It’s also pretty difficult. You can’t... | Read more »
NBA 2K17 (Games)
NBA 2K17 1.0 Device: iOS iPhone Category: Games Price: $7.99, Version: 1.0 (iTunes) Description: Following the record-breaking launch of NBA 2K16, the NBA 2K franchise continues to stake its claim as the most authentic sports video... | Read more »
Dog Sled Saga (Games)
Dog Sled Saga 1.0.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.0.1 (iTunes) Description: A game by Dan + Lisa As a rookie musher, foster a dogsledding team whose skills will grow if they're treated right. Week by... | Read more »
60 Seconds! Atomic Adventure (Games)
60 Seconds! Atomic Adventure 1.2 Device: iOS Universal Category: Games Price: $2.99, Version: 1.2 (iTunes) Description: 60 Seconds! is a dark comedy atomic adventure of scavenge and survival. Collect supplies and rescue your family... | Read more »
Tons of Bullets! (Games)
Tons of Bullets! 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Tons of BulletsA retro 2D pixel platformer infused with tons of bullets and tons of features!Fight as Kenji the Ninja and... | Read more »
Sorcery! 4 (Games)
Sorcery! 4 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: *** PLEASE NOTE: Sorcery! 4 requires a minimum of iPad 3 or iPhone 4 *** An epic adventure through a cursed citadel of monsters,... | Read more »
Building the perfect Animation Throwdown...
Animation Throwdown is a casual card game full of plenty of inside jokes from some of your favorite cartoons. It’s accessible as far as card games go, but that’s not to say that it hands your victories to you easily. There’s quite a bit that goes... | Read more »

Price Scanner via

Duplicate Sweeper Free On Mac App Store For O...
To celebrate the launch of Apple’s latest macOS Sierra, Stafford, United Kingdom based Wide Angle Software has announced that its duplicate file finder software, Duplicate Sweeper, is now available... Read more
13-inch Retina MacBook Pros on sale for up to...
B&H Photo has 13″ Retina Apple MacBook Pros on sale for up to $150 off MSRP. Shipping is free, and B&H charges NY tax only: - 13″ 2.7GHz/128GB Retina MacBook Pro: $1174.99 $125 off MSRP - 13... Read more
Evidence Surfaces Pointing To New A10X Chip F...
Citing a job description for a Project Lead position at Apple’s Austin, Texas engineering labs, Motley Fool’s Ashraf Eassa deduces that development is progressing well on Apple’s next-generation in-... Read more
Check Print’R for macOS Allows Anyone to Easi...
Delaware-based Match Software has announced the release and immediate availability of Check Print’R 3.21, an important update to their easy-to-use check printing application for macOS. Check Print’R... Read more
Apple refurbished 11-inch MacBook Airs availa...
Apple has Certified Refurbished 11″ MacBook Airs (the latest models), available for up to $170 off the cost of new models. An Apple one-year warranty is included with each MacBook, and shipping is... Read more
Apple refurbished 15-inch Retina MacBook Pros...
Apple has Certified Refurbished 2015 15″ Retina MacBook Pros available for up to $380 off the cost of new models. An Apple one-year warranty is included with each model, and shipping is free: - 15″ 2... Read more
Major Version 5 Release Of Free myTuner Radio...
AppGeneration Software has announced the release of myTuner Radio 5.0.1, an important update to their live radio app for iOS and Android platforms. With myTuner Radio, you can listen to more than 30,... Read more
TheAppStore Searches And Filters iOS And macO...
Boston based InchWest has announced the official launch of TheAppStore, a free web based service that lets users search iOS and Mac apps on the web without iTunes or the App store. TheAppStore... Read more
2TB Time Capsule on sale for $199, save $100
Amazon has 2TB Apple Time Capsules on sale for $199 including free shipping. Their price is $100 off MSRP. Choose Amazon as the seller when making your purchase, rather than a third party seller. Read more
12-inch 1.2GHz Rose Gold Retina MacBook on sa... has the 2016 12″ 1.2GHz Rose Gold Retina MacBook on sale for $1254.02 including free shipping. Their price is $345 off MSRP, and it’s the lowest price available for this model from any... Read more

Jobs Board

Senior *Apple* Administrator - Pratt Instit...
POSITION SUMMARY: Directs the coordination and standardization of campus-wide Apple systems, including planning, analysis and implementation of Apple -related Read more
Restaurant Manager (Neighborhood Captain) - A...
…in every aspect of daily operation. WHY YOU'LL LIKE IT: You'll be the Big Apple . You'll solve problems. You'll get to show your ability to handle the stress and Read more
*Apple* Retail - Multiple Positions- Napervi...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
US- *Apple* Store Leader Program - Apple (Un...
…Summary Learn and grow as you explore the art of leadership at the Apple Store. You'll master our retail business inside and out through training, hands-on Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.