[comp.sys.mac.programmer] How does one detect whether a modifier key is being held down?

monching@quiche.cs.mcgill.ca (Patrick WONG) (11/02/90)

Does anyone know if you can detect whether a modifier key is held down
without having to press a character key or clicking the mouse?

I know this is possible because MacPaint II will change the arrow to a
hand for option key and magifying glass for a command key.

I wish to do the samething.  Can't seem to find anything in Inside Mac.
I look up the stuff pertaining to keys, keyboard, and modifiers.  It just
says a modifier doesn't generate an event.  So does this mean I have to 
generate my own application event (appEvt) and check the modifier flag.  Or
is there a toolbox global where all key press are stored?

Thanks for any suggestions.

pat

-- 
patrick wong                   | McGill University           
monching@quiche.cs.mcgill.ca   | School of Computer Science                                | Montreal, Quebec            

hawley@adobe.COM (Steve Hawley) (11/03/90)

In article <5467@quiche.cs.mcgill.ca> monching@quiche.cs.mcgill.ca (Patrick WONG) writes:
>Does anyone know if you can detect whether a modifier key is held down
>without having to press a character key or clicking the mouse?
>
>I know this is possible because MacPaint II will change the arrow to a
>hand for option key and magifying glass for a command key.
>
>I wish to do the samething.  Can't seem to find anything in Inside Mac.

Look in Volume I on page 259.  The procedure GetKeys() returns the current
state of the keyboard.  In order to read it you could write something like
this:

IsThisKeyDown(keyCode)
register unsigned short keyCode;
{
	unsigned char myKeys[16];
	static unsigned char bits[8] = {
		0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1
	};

	GetKeys(myKeys);

	/* Address the bit in the key map.  The array "bits" might be
	 * reversed.  Try it out and see.
	 */
	return( (myKeys[keyCode / 8] & bits[keyCode % 8] ) != 0);
	/* if you want to play the efficiency game, you may want to
	 * rewrite this as:
	 * return( myKeys[keyCode >> 3] & bits[keyCode & 7]);
	 * which is equivalent code (albeit less readable) and will
	 * compile to more efficient code in Think C.
	 */
}

/* These values are take directly from inside Mac Volume I, page 251.  Please
 * note that the keymap values are different for many of the keys on
 * international keyboards!  Fortunately, the modifier keys are the same,
 * so the chances of the code retaining future compatability are good.
 * An example of a program that failed in the task is MacLanding (a shareware
 * Defender knockoff) that depended on the "Enter" key code staying the same.
 * It changed for the MacPlus and on, so the game doesn't play right.  Oops!
 */
#define IsOptionDown() IsThisKeyDown(58)
#define IsCommandDown() IsThisKeyDown(55)

Good luck.

Steve Hawley
hawley@adobe.com
-- 
"I'm sick and tired of being told that ordinary decent people are fed up with
being sick and tired.  I know I'm certainly not, and I'm sick and tired of
begin told that I am." -Monty Python

lim@iris.ucdavis.edu (Lloyd Lim) (11/03/90)

In article <5467@quiche.cs.mcgill.ca> monching@quiche.cs.mcgill.ca (Patrick WONG) writes:
>Does anyone know if you can detect whether a modifier key is held down
>without having to press a character key or clicking the mouse?
>
>I know this is possible because MacPaint II will change the arrow to a
>hand for option key and magifying glass for a command key.

In my main event loop, I just check the modifiers on every event and adjust
the cursor if necessary.  You may have to restrict yourself to certain events
or situations for adjusting the cursor, depending on what you're doing.  Note
that with MultiFinder, you should also adjust the cursor on mouse-moved events
(if it makes sense to do so).  It's convenient to to have one adjust cursor
routine to use for both cases.

+++
Lloyd Lim     Internet: lim@iris.ucdavis.edu (128.120.57.20)
              Compuserve: 72647,660
              US Mail: 215 Lysle Leach Hall, U.C. Davis, Davis, CA 95616

vrm@babcock.cerc.wvu.wvnet.edu (Vasile R. Montan) (11/03/90)

I accomplished this same thing by checking the modifiers each the program
cycled through its event loop.  You ordinarily have an if/then statement
using GetNextEvent.  GetNextEvent, a boolean function, returns FALSE if
the event is the Null Event (i.e., if there have been no events.) 
Nevertheless, the variables you pass to GetNextEvent are given the
correct values, so that your Modifiers variable will be set to reflect
the current state of the modifier keys. 

> I wish to do the samething.  Can't seem to find anything in Inside Mac.
> I look up the stuff pertaining to keys, keyboard, and modifiers.  It just
> says a modifier doesn't generate an event.  So does this mean I have to 
> generate my own application event (appEvt) and check the modifier flag.  Or
> is there a toolbox global where all key press are stored?

You don't have to mess with an application event.  Use an ELSE to catch
those cases where GetNextEvent returns FALSE, and use the Modifiers
variable however you want.  I had a NewModifiers and an OldModifiers
variable, so that I could respond to a change in the state of the keys.

(If you want to send mail to me, please send it to un020070@vaxa.wvnet.edu.
I'm not writing from home.)

--Kurisuto