monching@quiche.cs.mcgill.ca (Patrick WONG) (11/05/90)
Thanks to everyone how responded to my question. It came in really handy. To those out there in net-land, who might benefit from it. Here is the original post and the thread that followed. Remember each article is copyrighted by the author who wrote it. Plus it would be nice if they are given credit whenever possible if any of the code is used. Enjoy! ===================================================================== In article <monching@quiche.cs.mcgill.ca> you write: >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? ===================================================================== From: "Gerhard Mehldau" <gerhard@cs.arizona.edu> Subject: Re: How does one detect whether a modifier key is being held down? Try function GetKeys(), described in IM I, pp. 259-260. ===================================================================== From: rdd@ccwf.cc.utexas.edu (Robert Dorsett) Subject: Re: How does one detect whether a modifier key is being held down? If you use a GetNextEvent call and check the modifier flag, the event record modifier field will indicate the modifier keys' states, regardless of whether GetNextEvent returns TRUE or not. This is not documented, but I've never gotten it to fail. The alternative is to use the GetKeys function and check the key-map. ===================================================================== From: Michael Larsen <larsen@math.Princeton.EDU> Subject: Re: How does one detect whether a modifier key is being held down? Use the GetKeys() call, documented in the Event Manager chapter of IM I. ===================================================================== From: pratt@spot.Colorado.EDU (PRATT JONATHAN PETER) Subject: Re: How does one detect whether a modifier key is being held down? Procedure GetKeys(VAR theKeys: KeyMap); TYPE KeyMap = PACKED ARRAY [0..127] of BOOLEAN; In THINK C this is simply implemented as an array of four longints. If I remember correctly, all the modifier keys are represented in the second word. For example, to see if the command key is being held down, call GetKeys(theKeys) and then see whether theKeys.keys[1] & 0x8000 is non-zero - if so the key is down. (I might have the bits mixed up - I know that 0x8000, 1, and 2 are used. But there are four modifier keys, (option, shift, control, command) in there somewhere). Hope this helps some. ===================================================================== From: Johnny Lee <jlee4@lion.uwaterloo.ca> Subject: Re: How does one detect whether a modifier key is being held down? When you cal GetNextEvent or WaitNextEvent, the event record is returned with the modifiers field set to the current values of the modifier keys. Use them. ===================================================================== From: Richard Kennaway CMP RA <jrk@information-systems.east-anglia.ac.uk> Try GetKeys(), in IM vol.1. It gives you a bitmap showing the current state of every key, including the modifier keys. >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 I never thought of that. I hadnt realised that PostEvent fills in the modifiers, button state, etc. For some purposes this might be a better method, if it works, but for something like changing a cursor to reflect the modifiers, tracking GetKeys() would be more suitable. ===================================================================== From volaski@contra.med.buffalo.edu Fri Nov 2 10:20:21 1990 From: volaski@contra.med.buffalo.edu (Maurice Volaski) Subject: Re: How does one detect whether a modifier key is being held down? One way would be to use GetKeys in your main event loop. This procedure fills in a KeyMap letting you know which keys are down. I believe the raw key codes are given in IM. You can use the following formula and code to check for a key: GetKeys(&keyMap)/*keyMap is of type KeyMap*/ offset=MODIFIER+7-2*(MODIFIER%8) /*MODIFIER is a raw key code*/ if(BitTst(keyMap,offset)) /*the MODIFIER is down*/ ===================================================================== From: dorner@pequod.cso.uiuc.edu (Steve Dorner) Subject: Re: How does one detect whether a modifier key is being held down? It's very easy. Just call GetNextEvent with an event mask of 0; you'll get back a null event, but the modifiers field will be set properly. You can look at them to see if a modifier key is down. ===================================================================== From: David Hairston <hairston@henry.ece.cmu.edu> Subject: Re: modifier keys you should be able to read the modifier fields of the event record during null events. in this way you can constantly track the status of the modifier keys. ref. Inside Mac I, Toolbox Events. ===================================================================== From: dowdy@apple.com (Tom Dowdy) Subject: Re: How does one detect whether a modifier key is being held down? All events, including nullEvents, have a modifiers field in them. Just look there. ===================================================================== From: jrb@mbunix.mitre.org (Boonstra) Subject: Modifiers Suggest using GetKeys (IM I-259). Doesn't generate an event, but you can check it going thru the event loop. ===================================================================== From: lippin@math.berkeley.edu (Tom Lippincott) Subject: Re: How does one detect whether a modifier key is being held down? When you get a null event, it has the modifier bits set. I think that's how MacPaint finds out. I believe you can also ask directly, using GetKeys. ===================================================================== From: David Byers <c90davby@odalix.ida.liu.se> Subject: How does one detect whether a modifier key is being held down? Call GetKeys(), and look through IM V (script manager?) to find the correct key codes for the modifier keys. Key codes for the Mac+ keyboards are listed in older volumes as well. ===================================================================== From: <DN5%PSUVM.PSU.EDU@vm1.mcgill.ca> (D. Jay Newman) Subject: Re: How does one detect whether a modifier key is being held down? Hi: You should be getting NULL events, and even nullEvents have the modifiers part of the event record. Don't ignore them just because they normally don't tell you anything! ===================================================================== From: d88-jwa@nada.kth.se (h+) Subject: Re: How does one detect whether a modifier key is being held down? #define optionDown ( KeyIsDown ( 0x3A ) ) Other key codes can be found in IM V ===================================================================== From: d88-jwa@nada.kth.se (h+) Subject: Re: How does one detect whether a modifier key is being held down? Oh, KeyIsDown() is part of the file OSChecks.c in the Think Class Library, so my test presupposes THINK C 4.0... GetKeys should work, anyways. See IM. ===================================================================== From: rmh@apple.com (Rick Holzgrafe) Subject: Re: How does one detect whether a modifier key is being held down? Yes, you can. In your main event loop (or wherever you get regular time) do the following: KeyMap theKeys; #define OPTION_DOWN (theKeys.Key[1] & 0x04) #define CMD_DOWN (theKeys.Key[1] & 0x8000) GetKeys (&theKeys); /* See what keys are down */ if ( OPTION_DOWN ) DoSomething(); if ( CMD_DOWN ) DoSomethingElse(); GetKeys is documented in Inside Macintosh Vol. I Pp. 259-260. Interpreting the result is easy in Pascal, since there's a datatype for it. It's harder in C, but the above code works. ===================================================================== From: <Invader%cup.portal.com%portal.UUCP%claris.UUCP%ames.UUCP%ames.UUCP%uunet.UUCP@Larry.McRCIM.McGill.EDU> GetKeys followed by a BitTst will do it. 55 = command 56 = shift 57 = caps lock 58 = option 59 = control mkd -- patrick wong | McGill University monching@quiche.cs.mcgill.ca | School of Computer Science | Montreal, Quebec
lsr@Apple.COM (Larry Rosenstein) (11/06/90)
monching@quiche.cs.mcgill.ca (Patrick WONG) writes: >If you use a GetNextEvent call and check the modifier >flag, the event record modifier field will indicate the modifier keys' >states, regardless of whether GetNextEvent returns TRUE or not. This is not >documented, but I've never gotten it to fail. One can also use the equivalent OS Event Manager call (GetOSEvent), which does less work than GetNextEvent. (Also, unlike GetNextEvent it doesn't move memory, which means it can be used at interrupt level.) -- Larry Rosenstein, Object Specialist Apple Computer, Inc. 20525 Mariani Ave, MS 3-PK Cupertino, CA 95014 AppleLink:Rosenstein1 domain:lsr@Apple.COM UUCP:{sun,voder,nsc,decwrl}!apple!lsr