Jun 95 Tips
Volume Number: | | 11
|
Issue Number: | | 6
|
Column Tag: | | Tips & Tidbits
|
Tips & Tidbits
By Neil Ticktin, Editor-in-Chief with special thanks to Steve Sisak
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
TIP OF THE MONTH
GetKeys From Within MacsBug
There is a routine in the Macintosh toolbox called GetKeys. This routine is great for game programmers who want a reasonably fast way to read the keyboard, without using (slower) events. The problem for C and C++ programmers using this routine is that the KeyMap type is a Pascal packed array. Each bit of the packed array is designated to a certain key, the bit is set to 1 if the key is down, and set to zero if the key is up. This array takes up 16 bytes (128 bits). C cannot access the elements of the packed array like a normal array, so the programmer has to mask out some bits to get the result that he/she wants. There is a desk accessory named GetKeys, that is made just for this case. The problem is, you might not be on a machine with that program on it.
Good thing MacsBug is able to help us. Here is how you locate the bit for the letter M: go into MacsBug and type in dm KeyMap, but dont hit return just yet. Now strike the escape key, this should swap the screen. Press and hold down the letter M on your keyboard, this should swap the screen back. Now, while still holding down M, press return. This is what you should see:
Displaying memory from 0174:
00000174 0000 0000 0040 0000 0000 0000 0000 0000 @
The number 00000174 is the address of the KeyMap global variable. The next set of numbers 0000 0000 is the first element of the C version of the array, in other words its: keyMap[0]. The next set of numbers 0040 0000 is the second element of the array, keyMap[1]. The next group of 8 hexadecimal digits is the third element (keyMap[2]), and the last group of 8 hexadecimal digits is the fourth element (keyMap[3]). The series of bullets is what the array looks like in ASCII form. In the second group (0040 0000) there is a 4 in the midst of all of those zeros. This is the bit that is set to 1 whenever the M key is held down. So, to see if the M is down from within C we will do this:
KeyMap keyMap;
GetKeys( keyMap );
if( keyMap[1] & 0x00400000 )
{ DoMKeyDown();
}
Thats all there is to it!
Macneil Shonle, Yarmouth Maine
MacneilS@aol.com
Almost, But Not Quite
In his tip (MacTech, March 1995 issue, page 67), Jeff Beeghly assumes items #1 and #2 to be respectively the default and the cancel items. That is not always true: in fact these may even change between subsequent calls to the same dialog/alert if the programmer used SetDialogDefaultItem and SetDialogCancelItem before calling ModalDialog or set the ALRT stage info in ResEdit for the alert box. The safest way to find the default item is to look at the aDefItem field of the DialogRecord that is passed to the the custom modal filter procedure.
Moreover, I think it is best to let the original filter procedure track standard events were not interested in, by calling it via StdFilterProc from our custom filter proc and then returning its return value to the application.
- Luigi Belverato