Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!att!linac!unixhub!slacvm!toge From: TOGE@SLACVM.SLAC.STANFORD.EDU (Nobukazu Toge) Newsgroups: comp.sys.mac.programmer Subject: Re: Raw keyCode ==> ASCII? Message-ID: <91103.162951TOGE@SLACVM.SLAC.STANFORD.EDU> Date: 14 Apr 91 00:29:51 GMT References: <1991Apr13.165337.29678@kuhub.cc.ukans.edu> Organization: Stanford Linear Accelerator Center Lines: 43 I had an experience converting KeyCodes into Ascii codes, or something that is readily display-able. A problem here is that there is no single characters characters "left-arrow", "home", "enter", "F1" etc. So as you are afraid, it gets pretty messy. It looks as if you need to hard-code (or at the best, make it driven by your custom-made resource) to handle F-keys etc extensions. I, too, would like to know if there's a more elegant solution. Part of the code I'm using follows - void Key2Char(short myKey, Str255 *chars) /* myKey is the KeyCode (input), chars is the output char string */ { SysEnvRec theWorld; short theErr; Handle myHandle; Ptr myPointer; short qChar; short kchrID; theErr = SysEnvirons(1, &theWorld); kchrID = GetScript(GetEnvirons(smKeyScript), smScriptKeys); myHandle = GetResource('KCHR',kchrID); /* Strictly speaking you'd better check myhandle != NIL but I'm lazy */ qChar = 0xFF; myPointer = *myHandle+260+myKey; qChar = (short)(*myPointer) & 0xFF; if ((qChar >= 0x21) && (qChar <= 0x7E) && (qChar != 0x7F)) { if (myKey < 0x40) { (*chars)[0] = 1; (*chars)[1] = qChar; } /* You need to handle keypad key-characters by hand */ /* In my case, I did '1' --> '[1]' if it's from the keypad */ } /* You need to handle other "weird" character cases by hand */ /* In my case, I did a grand switch/case coding here */ }