[comp.sys.mac.programmer] KeyUp Events - How?

hammersslammers1@oxy.edu (David J. Harr) (09/03/90)

I am writing a game where the keypad is used to control a vehicle. Among
other capabilities, I'd like to be able to have the user press down on the
[5] key, and as long as they hold it down, have the vehicle continue to fire
weapons. Currently, I check the event.what field for a keyDown or autoKey
event. However, as soon as another key is pressed to move the vehicle or do
something else, the event.message field no loger contains the keycode of the
keypad [5]. This is as expected, and I have no problem with that. However,
it seems to me that as long as the user holds down the [5] key, there will
be no keyUp event. So what I was doing was setting a flag to enable autofire
to true until such time as a keyUp event with the keycode for the [5] key
was received. The relevant code looks like:
for (;;)
{
    ok = WaitNextEvent(everyEvent, &evt, OL, wp);
    if (ok) /* if a null event, just skip the whole event handling routine*/
    {
	switch(evt.what)
	{
	    case keyDown:
		keycode = BitAnd(evt.message, KeyCodeMask);/* find keycode*/
		... (various keycode handling routines)
		else if (keycode == 0x5700) /*0x5700 is keypad 5*/
		{
		     (Routine to animate firing weapons)
		}

		break;

	    case autoKey:
		keycode = BitAnd(evt.message, KeyCodeMask);
		... (various keycode handling routines)
		else if (keycode == 0x5700) /*user continuing to press the*/
		{			   /*fire button, enable autofire */
		autofire = true;
		(Routine to animate firing weapons)
		}

		break;

	    case keyUp:
		if (BitAnd(evt.message,KeyCodeMask) == 0x5700
		    autofire = false; /*should stop the firing sequence */
		break;
       }
    }
    if(autofire)
      (routine to animate firing weapons)
    ...

My logic is that if the autofire flag is true, even if the computer is
reporting null events, the user is still holding on to the [5] key, but he
has presses some other key since then and released it. As long as he is
holding down the [5] key, however, I want to keep up the animation. My
problem is this: As fas as I can tell, my loop never receives a keyUp event.
If two keys are held down at the same time and then released seperately,
shouldn't two separate key up events be posted? I've enabled everyEvent when
I call WNE, so I am thouroughly confused. Has anyone else had any experience
working with these types of events? Any light anyone can shed on this would
greatly appreciated.

David

rgm@sandstorm.Berkeley.EDU (Rob Menke) (09/03/90)

In article <109041@tiger.oxy.edu> hammersslammers1@oxy.edu (David J.
Harr) writes:

   ...As fas as I can tell, my loop never receives a keyUp event.
   If two keys are held down at the same time and then released seperately,
   shouldn't two separate key up events be posted? I've enabled everyEvent when
   I call WNE, so I am thouroughly confused. Has anyone else had any experience
   working with these types of events? Any light anyone can shed on this would
   greatly appreciated.

   David

The problem here is not the toolbox event manager, it's the OS event
manager.  Since keyUp events aren't used often, the OS event manager
kindly filters them out.

"You should use SetEventMask only to enable key-up events in the
unusual case that your application needs to respond to them."
(IM v. II, p. 70)

So adding the line "SetEventMask(-1);" before your event loop should
make everything work correctly.
--
"Gadget, love, do ya always carry a	|  Robert Menke
 glass cutter?"				|    rgm@OCF.berkeley.edu
"No-- only when I want to cut glass."	|    ...!ucbvax!OCF!rgm