As I mentioned in my other message, here’s a quick snippet for reading
the raw keymap and translating into cooked characters on the MacOS.
Matt
-------------- next part --------------
/* Function Prototypes */
extern short GetPressedKeys(void);
extern long
SimpleKeyTrans(short keycode, long modifiers);
/* Magic formula to test a
bit in the keymap */
#define IsKeyPressed(km,kc) ((km[kc/8] & (1L <<
kc%8)) ? true : false)
/* Typical charcode constants */
#define
kLeftArrowChar 0x1C,
#define kRightArrowChar
0x1D,
#define kUpArrowChar 0x1E,
#define
kDownArrowChar 0x1F,
#define kPageUpChar
0x0B,
#define kPageDownChar 0x0C,
#define kHomeChar
0x01,
#define kEndChar 0x04,
#define
kEnterChar 0x03,
#define kReturnChar
0x0D,
#define kForwardDeleteChar 0x7F,
#define kDeleteChar
0x08,
#define kHelpChar
0x05,
#define kTabChar 0x09,
/* These keycodes
have non-unique charcodes */
#define kEscapeKey
0x35,
#define kClearKey 0x47,
#define kF1Key
0x7A,
#define kF2Key
0x78,
#define kF3Key 0x63,
#define
kF4Key 0x76,
#define kF5Key
0x60,
#define kF6Key 0x61,
#define
kF7Key 0x62,
#define kF8Key
0x64,
#define kF9Key 0x65,
#define
kF10Key 0x6D,
#define kF11Key
0x67,
#define kF12Key 0x6F,
#define
kF13Key 0x69,
#define kF14Key
0x6B,
#define kF15Key 0x71,
/*
Standard modifier keycodes */
#define kCommandKey
0x37,
#define kShiftKey 0x38,
#define kCapsLockKey
0x39,
#define kOptionKey
0x3A,
#define kControlKey 0x3B,
short
GetPressedKeys() {
long modifiers;
short
keycode; /* Virtual (raw) key code /
short
keychar; / ASCII (cooked) character /
short
deadchar; / Accent character (optional) */
short
count = 0;
long result;
/* GetKeys() is more
compatible, but reading lowmem is faster */
#if 1
unsigned char
km[16];
GetKeys((unsigned long *) km);
#else
unsigned char *km =
(unsigned char *) 0x0174;
#endif
/* Event manager constants to
describe which modifier keys are pressed */
modifiers = 0;
if
(IsKeyPressed(km, kShiftKey)) modifiers |= shiftKey;
if
(IsKeyPressed(km, kOptionKey)) modifiers |= optionKey;
if
(IsKeyPressed(km, kCommandKey)) modifiers |= cmdKey;
if
(IsKeyPressed(km, kControlKey)) modifiers |= controlKey;
if
(IsKeyPressed(km, kCapsLockKey)) modifiers |= alphaLock;
for(keycode = 0; keycode < 128 /* 16 bytes */ ; keycode++) {
if (IsKeyPressed(km, keycode)) {
/* Pass the
keycode and modifers, as described in IM. */
result =
SimpleKeyTrans(keycode, modifiers);
/* Dissect the result into one or two characters */
if (result & 0xFFFF0000) {
keychar =
(ktResult & 0xFFFF0000) >> 16;
deadchar = ktResult
& 0x0000FFFF;
}
else {
keychar = ktResult & 0x0000FFFF;
deadchar = 0;
}
/* We found another pressed key -- record it */
count++;
}
}
return(count);
}
/*
[From THINK Reference]
The
’KCHR’ resource specifies the mapping of virtual key codes to
character
codes (for example, ASCII). Each installed script system has one
or more
’KCHR’ resources; there may be one or more for each language or
region to
suit the preference of the user. The resource ID for each 'KCHR’
resource
is in the script’s resource number range. The default 'KCHR’
resource for a
script is specified by the script’s ‘itlb’ resource.
Also
see IM: Toolbox Essentials, 2-110 to 2-111 and IM: Text, C-22 to
C24.
*/
/* A simple wrapper for KeyTrans() that doesn’t need a ‘KCHR’
*/
long SimpleKeyTrans(short keycode, long modifiers) {
static
unsigned long kchrState = 0;
static Ptr kchrPtr = NULL;
Ptr
kchrNew = NULL;
long result =
0;
/* The script manager allows access to its internal 'KCHR'
data */
kchrNew = (Ptr) GetScriptManagerVariable(smKCHRCache);
if
(kchrPtr != kchrNew) {
kchrPtr = kchrNew;
kchrState =
0;
}
/* Pass the combined keycode and modifers,
as described in IM. */
return(KeyTranslate(kchrPtr, keycode | modifiers,
&kchrState));
}
-------------- next part --------------
/* Matt Slot, Bitwise Operator * One box, two box, yellow box, blue box. *