Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!ucsd!pacbell.com!decwrl!adobe!hawley From: hawley@adobe.COM (Steve Hawley) Newsgroups: comp.sys.mac.programmer Subject: Re: How does one detect whether a modifier key is being held down? Keywords: Modifier key Message-ID: <7919@adobe.UUCP> Date: 2 Nov 90 17:28:23 GMT References: <5467@quiche.cs.mcgill.ca> Reply-To: hawley@adobe.UUCP (Steve Hawley) Organization: Adobe Systems Incorporated, Mountain View Lines: 54 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