appletkb.c

Go to the documentation of this file.
00001 
00002 /*  $Id: appletkb.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /* copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
00005 
00006 
00007 #include "appletmemory.h"
00008 #include "appletcursor.h"
00009 #include "appletkb.h"
00010 
00011 
00012 
00013 #define keycodeclear 71 /*keycodes for numeric keybad*/
00014 #define keycodeminus 78
00015 #define keycodeplus 69
00016 #define keycodetimes 67
00017 #define keycodeseven 89
00018 #define keycodeeight 91
00019 #define keycodenine 92
00020 #define keycodedivide 77
00021 #define keycodefour 86
00022 #define keycodefive 87
00023 #define keycodesix 88
00024 #define keycodecomma 72
00025 #define keycodeone 83
00026 #define keycodetwo 84
00027 #define keycodethree 85
00028 #define keycodeenter 76
00029 #define keycodezero 82
00030 #define keycodeperiod 65
00031 
00032 
00033 tykeystrokerecord keyboardstatus; 
00034 
00035 static boolean flescapepending = false;
00036 
00037 
00038 
00039 
00040 boolean arrowkey (char chkb) {
00041     
00042     /*
00043     return true if the indicated key character is an arrow key.
00044     */
00045     
00046     char ch = chkb;
00047     
00048     return (
00049         (ch == chuparrow) || (ch == chdownarrow) || 
00050         
00051         (ch == chleftarrow) || (ch == chrightarrow));
00052     } /*arrowkey*/
00053 
00054 
00055 tydirection keystroketodirection (char ch) {
00056     
00057     switch (ch) {
00058         
00059         case chleftarrow:
00060             return (left);
00061             
00062         case chrightarrow:
00063             return (right);
00064             
00065         case chuparrow:
00066             return (up);
00067             
00068         case chdownarrow:
00069             return (down);
00070         } /*switch*/
00071         
00072     return (nodirection);
00073     } /*keystroketodirection*/
00074     
00075 
00076 static void kbsetstatus (EventRecord ev, tykeystrokerecord *kbs) {
00077     
00078     /*
00079     12/11/90 dmb: don't count the caps lock as a modifier in ctmodifiers.
00080     
00081     2/21/91 dmb: handle calls for mousedown events.  we just want to 
00082     record modifier keys
00083     */
00084     
00085     short ct;
00086     char chkb;
00087     short keycode;
00088     tykeystrokerecord kbcurrent;
00089     
00090     kbcurrent.flshiftkey = (ev.modifiers & shiftKey) != 0;
00091     
00092     kbcurrent.flcmdkey = (ev.modifiers & cmdKey) != 0;
00093     
00094     kbcurrent.floptionkey = (ev.modifiers & optionKey) != 0;
00095     
00096     kbcurrent.flalphalock = (ev.modifiers & alphaLock) != 0;
00097     
00098     kbcurrent.flcontrolkey = (ev.modifiers & controlKey) != 0;
00099     
00100     ct = 0;
00101     
00102     if (kbcurrent.flshiftkey) ct++;
00103     
00104     if (kbcurrent.flcmdkey) ct++;
00105     
00106     if (kbcurrent.floptionkey) ct++;
00107     
00108     /*
00109     if (kbcurrent.flalphalock) ct++;
00110     */
00111     
00112     if (kbcurrent.flcontrolkey) ct++;
00113     
00114     kbcurrent.ctmodifiers = ct; 
00115     
00116     if (ev.what == mouseDown) {
00117         
00118         kbcurrent.chkb = chnul;
00119         
00120         kbcurrent.keycode = 0;
00121         
00122         kbcurrent.flautokey = false;
00123         
00124         kbcurrent.keydirection = nodirection;
00125         }
00126     else {
00127         
00128         kbcurrent.chkb = chkb = ev.message & charCodeMask; /*get the keystroke*/
00129         
00130         kbcurrent.flautokey = (ev.what == autoKey);
00131         
00132         kbcurrent.keycode = keycode = (ev.message & keyCodeMask) >> 8;
00133         
00134         kbcurrent.keydirection = keystroketodirection (chkb);
00135         
00136         if (kbcurrent.flcmdkey  &&  kbcurrent.floptionkey) {
00137     
00138             /*
00139             we don't want to option character, so find the normal character from 
00140             the keymap.  see IM V-195
00141             */
00142             
00143             Handle hkchr;
00144             unsigned long state = 0;
00145             
00146             if ((hkchr = GetResource ('KCHR', 0)) != NULL)
00147                 kbcurrent.chkb = KeyTrans (*hkchr, kbcurrent.keycode, &state) & 0x000000ff;
00148             };
00149     
00150         kbcurrent.flkeypad = /*true if it is a keystroke from the numeric keypad*/
00151             
00152             (keycode == keycodeclear)       || (keycode == keycodeminus)    || 
00153             
00154             (keycode == keycodeplus)        || (keycode == keycodetimes)    || 
00155             
00156             (keycode == keycodeseven)       || (keycode == keycodeeight)    ||
00157             
00158             (keycode == keycodenine)        || (keycode == keycodedivide)   ||
00159             
00160             (keycode == keycodefour)        || (keycode == keycodefive)     ||
00161             
00162             (keycode == keycodesix)         || (keycode == keycodecomma)    ||
00163             
00164             (keycode == keycodeone)         || (keycode == keycodetwo)      ||
00165             
00166             (keycode == keycodethree)       || (keycode == keycodeenter)    ||
00167             
00168             (keycode == keycodezero)        || (keycode == keycodeperiod);
00169         }
00170     
00171     *kbs = kbcurrent; /*set for caller*/
00172     } /*kbsetstatus*/
00173     
00174 
00175 void setkeyboardstatus (EventRecord ev) {
00176     
00177     /*
00178     sets a global that may be referenced by anyone.
00179     
00180     should be called every time a new event is received.
00181     */
00182     
00183     kbsetstatus (ev, &keyboardstatus); /*use global*/
00184     } /*setkeyboardstatus*/
00185 
00186 
00187 void keyboardclearescape (void) {
00188     
00189     flescapepending = false;
00190     } /*keyboardclearescape*/
00191 
00192 
00193 boolean keyboardescape (void) {
00194     
00195     /*
00196     check to see if the user has pressed cmd-period.  if not, no effect on
00197     the event queue and we return false.
00198     
00199     otherwise, we remove the keystroke and return true.  the caller is expected
00200     to return quickly!
00201     
00202     DW 8/18/93: also handle Esc key.
00203     
00204     DW 8/30/93: the manual can say that you can press cmd-period or Esc 
00205     whenever you see the beachball rolling.
00206     
00207     DW 9/16/93: check once per second.
00208     */
00209     
00210     if (flescapepending)
00211         return (true);
00212         
00213     rollbeachball (); /*DW 8/30/93*/
00214         
00215     /*return false if it's been less than 1 second since we checked*/ {
00216         
00217         static unsigned long tclastcheck = 0;
00218         unsigned long tc;
00219         
00220         tc = TickCount ();
00221         
00222         if ((tc - tclastcheck) < 60)
00223             return (false);
00224         
00225         tclastcheck = tc; /*... and fall thru*/
00226         }
00227     
00228     /*check the event queue for an Esc or cmd-period*/ {
00229     
00230         EventRecord ev;
00231         
00232         if (EventAvail (keyDownMask, &ev)) {
00233             
00234             tykeystrokerecord kbcurrent;
00235     
00236             kbsetstatus (ev, &kbcurrent);
00237             
00238             if (kbcurrent.chkb == chescape) /*Esc key*/
00239                 flescapepending = true;
00240                 
00241             if (kbcurrent.flcmdkey && (kbcurrent.chkb == '.'))
00242                 flescapepending = true;
00243                 
00244             if (flescapepending)        
00245                 GetNextEvent (keyDownMask, &ev); /*get rid of the Esc keystroke*/
00246             } 
00247         }
00248     
00249     return (flescapepending);
00250     } /*keyboardescape*/
00251 
00252 
00253 void keyboardpeek (tykeystrokerecord *kbrecord) {
00254     
00255     ptrkeystrokerecord p = kbrecord;
00256     KeyMap keys;
00257     
00258     clearbytes (p, longsizeof (tykeystrokerecord));
00259     
00260     GetKeys (keys);
00261     
00262     (*p).flshiftkey = BitTst (&keys, 63);
00263         
00264     (*p).flcmdkey = BitTst (&keys, 48);
00265         
00266     (*p).floptionkey = BitTst (&keys, 61);
00267         
00268     (*p).flcontrolkey = BitTst (&keys, 60);
00269     } /*keyboardpeek*/
00270 
00271 
00272 static boolean keydown (short keycode) {
00273 
00274     KeyMap keys;
00275     
00276     GetKeys (keys);
00277     
00278     return (BitTst (&keys, keycode));
00279     } /*keydown*/
00280     
00281     
00282 boolean enterkeydown (void) {   
00283     
00284     return (keydown (75));
00285     } /*enterkeydown*/
00286     
00287     
00288 boolean optionkeydown (void) {
00289     
00290     return (keydown (61));
00291     } /*optionkeydown*/
00292     
00293     
00294 boolean cmdkeydown (void) {
00295         
00296     return (keydown (48));
00297     } /*cmdkeydown*/
00298     
00299     
00300 boolean shiftkeydown (void) {
00301         
00302     return (keydown (63));
00303     } /*shiftkeydown*/
00304     
00305     
00306 boolean returnkeydown (void) {
00307     
00308     /*
00309     short i; 
00310     bigstring bs;
00311     
00312     for (i = 1; i <= 200; i++) {
00313     
00314         if (keydown (i)) {
00315         
00316             NumToString (i, bs);
00317             
00318             DebugStr (bs);
00319             }
00320         }
00321     */
00322         
00323     return (keydown (35)); 
00324     } /*returnkeydown*/
00325     
00326     
00327     

Generated on Wed May 31 18:19:42 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6