home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / Cancel_key.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  2.3 KB  |  77 lines  |  [TEXT/R*ch]

  1. /*
  2.     This code is taken from Tech Note 263, "International Cancelling."
  3.     However, unlike that note, this code assumes that we have already
  4.     identified the event as a keyboard event, and uses the escape key
  5.     as a synonym for command-period.
  6. */
  7. #include <Script.h>
  8.  
  9. Boolean Cancel_key( EventRecord *theEvent );
  10.  
  11. #define kMaskModifiers  0xFE00     // we need the modifiers without the
  12.                                    // command key for KeyTrans
  13. #define kMaskVirtualKey 0x0000FF00 // get virtual key from event message
  14.                                    // for KeyTrans
  15. #define kUpKeyMask      0x0080
  16. #define kShiftWord      8          // we shift the virtual key to mask it
  17.                                    // into the keyCode for KeyTrans
  18. #define kMaskASCII1     0x00FF0000 // get the key out of the ASCII1 byte
  19. #define kMaskASCII2     0x000000FF // get the key out of the ASCII2 byte
  20. #define kPeriod         0x2E       // ascii for a period
  21. #define kEscape            0x1B        // ascii for escape
  22.  
  23. Boolean Cancel_key( EventRecord *theEvent )
  24. {
  25.  
  26.   Boolean    fTimeToQuit;
  27.   short        keyCode;
  28.   long        virtualKey, keyInfo, lowChar, highChar, state, keyCId;
  29.   Handle    hKCHR;
  30.   Ptr        KCHRPtr;
  31.  
  32.   fTimeToQuit = false;
  33.  
  34.   // see if the command key is down.  If it is, find out the ASCII
  35.   // equivalent for the accompanying key.
  36.  
  37.   if ((*theEvent).modifiers & cmdKey ) {
  38.  
  39.     virtualKey = (theEvent->message & kMaskVirtualKey) >> kShiftWord;
  40.     // And out the command key and Or in the virtualKey
  41.     keyCode    = ((*theEvent).modifiers & kMaskModifiers)  |  virtualKey;
  42.     state      = 0;
  43.  
  44.     hKCHR = nil;  /* set this to nil before starting */
  45.     KCHRPtr = (Ptr) GetEnvirons( smKCHRCache );
  46.  
  47.     if ( !KCHRPtr ) {
  48.       keyCId = GetScript( GetEnvirons(smKeyScript), smScriptKeys);
  49.  
  50.       hKCHR   = GetResource('KCHR',keyCId);
  51.       KCHRPtr = *hKCHR;
  52.     }
  53.  
  54.     if (KCHRPtr)
  55.     {
  56.       /* Don't bother locking since KeyTrans will never move memory */
  57.       keyInfo = KeyTrans( KCHRPtr, keyCode, &state );
  58.       ReleaseResource( hKCHR );
  59.     }
  60.     else
  61.      keyInfo = theEvent->message;
  62.  
  63.     lowChar =  keyInfo &  kMaskASCII2;
  64.     highChar = (keyInfo & kMaskASCII1) >> 16;
  65.     if (lowChar == kPeriod || highChar == kPeriod)
  66.       fTimeToQuit = true;
  67.  
  68.   }  // end the command key is down
  69.   else
  70.   {
  71.       if ( (theEvent->message & charCodeMask) == kEscape )
  72.         fTimeToQuit = true;
  73.   }
  74.  
  75. return( fTimeToQuit );
  76. }
  77.