Newsgroups: comp.sys.mac.programmer
Subject: Re: Option Key Madness
From: richardk@kiwi.gen.nz (Richard Knuckey)
Date: 16 Oct 92 05:17:22 GMT
Organization: Unix Usenet Server, Western Springs, Auckland, New Zealand
Keywords: option Meyremapping mac
Summary: How to map control->option key on mac


> 	The wrong way is what ZTerm & NCSA Telnet (to name two) do it. That
> is to require the user to use THEIR KCHR resource, which requires
> the user to  do a bit of ResEditting of the System file, since
> KCHR's must live in the System under System 7.
> 
> 	The right way to do it (which MicroPhone II & Smartcom do) is to 
> first patch _KeyTrans to map the option modiifier to something else
> (in the cases above, the control modifieir).  However, since the
> process manager swaps patches on even minor context switches, you
> could STILL get the wrong characters.  So you have to do some
> on-the-fly munging of the KCHR (after copying the orignal!!) - BUT
> remember to restore the original KCHR on  suspend/resumes!!
 
	
No No No! Since when do you have to _patch_ the OS to remap the keyboard.
Never ever patch anything unless you absoulety *have* to.
 
All you do is call _KeyTrans yourself (see Inside Mac V) with your own
KCHR structure in memory. This is what I do in Moria when I detect a
macPlus Keyboard. Code follows:
		
		 
/* Variables */

extern	Handle	KCHRresource;
extern	long	trans_state;
 


/* From InitWimp() - Code to set up KCHR and trans_state */
 
if (g->KeyboardType <= envMacPlusKbd)
    {
    trans_state = 0;
    KCHRresource = GetResource('KCHR', 128);
    MoveHHi(KCHRresource);
    HLock(KCHRresource);
    }
 
 
 
/* ------------------------------------------------------------------------
 * RemapKeys
 *
 * This remaps the keyboard using the KCHR we loaded in initwimp.
 * It calls KeyTrans, to translate vitual keycodes to ASCII values.
 * This is only called on keyboards without an Escape
 * or control keys such as on a MacPlus. The KCHR maps the control key
 * to the option key and the tilde key to the escape key. This will work
 * even under system 7.0 since we are forcing a keytranslation instead
 * of simply setting the system wide KCHR, which under system 7.0 requires
 * the KCHR resource to be in the system for reasons discussed in
 * Inside Macintosh VI pp 14-96
 */
 
RemapKeys(theEvent)
EventRecord *theEvent;
    {
    short   keycode;
    char    *p, *q;     /* used for byte level acces to variables */
    long    ascii_code;
    
    p = &keycode;
    
    q = &theEvent->message;
    p[1] = q[2];
    
    q = &theEvent->modifiers;
    p[0] = q[0];
    
    ascii_code = KeyTrans(*KCHRresource,keycode,&trans_state);
    
    p = &ascii_code;
    theEvent->message = p[3];
    }
 
 
/* From Window Handler for main Moria game window */
 
case keyDown:
case autoKey:
    ObscureCursor();
    if (g->KeyboardType <= envMacPlusKbd)
        RemapKeys ( theEvent );
    
    theChar = (char)(theEvent->message & charCodeMask);
    
 
The Biggest advantages of this way is that it doesn't muck around with
the rest of the system, It only remaps the keyboard for the window
that needs it, I doesn't require the instalation of any resources to the
system file and the KCHR does't show up in the Keyboard menu.

A disadvantage is that the system does not pass on "dead keys" to you. this
means that you won't get an option-e passed to you, since it's marked as a dead
key in the default US system KCHR. This may casue you concern. Luckely Moria
doesn't use any on these keys anyway :-). The only clean way around this
second problem if you absoluetly MUST have option-e mapped to something is yo
havethe user install a keyboard layout, and for you to switch to that layout on
resume events ,and to set it back on suspend events.

Newsgroups: comp.sys.mac.programmer