home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk436.lzh / Input / input.c < prev    next >
C/C++ Source or Header  |  1991-01-15  |  5KB  |  156 lines

  1. /********************************************
  2. *  INPUT.C  08/04/90
  3. *  © Copyright 1990 Timm Martin
  4. *  This source code is freely distributable
  5. *  and may be used without compensation in
  6. *  any commercial or non-commercial product
  7. *  as long as this notice is included and
  8. *  remains intact.  This code may be used
  9. *  in an executable program without
  10. *  acknowledgement of the author.
  11. *********************************************/
  12.  
  13. #include <devices/inputevent.h>
  14. #include <exec/types.h>
  15. #include <intuition/intuition.h>
  16. #include <functions.h>
  17. #include "input.h"
  18.  
  19. /*************
  20. *  INPUT KEY
  21. **************/
  22.  
  23. /*
  24. Given a pointer to the IntuiMessage structure received for the current window
  25. by Intuition, this function will return the key that was pressed, or zero if
  26. no valid key was pressed.
  27. */
  28.  
  29. #define QUAL ievent.ie_Qualifier
  30. #define SIZE 20L
  31.  
  32. USHORT input_key( struct IntuiMessage *imessage )
  33. {
  34.   UBYTE  buffer[SIZE];  /* buffer into which raw key conversion is read */
  35.   short  codes = 0;     /* number of ANSI codes returned */
  36.   static struct InputEvent ievent = { NULL, IECLASS_RAWKEY, 0, 0, 0 };
  37.   USHORT key = 0;       /* key value returned by this function (see below) */
  38.   USHORT qual = 0;      /* key qualifiers (shift, alt, ctl, etc.) */
  39.  
  40.   /* copy rawkey codes and qualifiers into InputEvent structure for conversion */
  41.   ievent.ie_Code = imessage->Code;
  42.   QUAL = imessage->Qualifier;
  43.  
  44. /*
  45.  
  46. LG = left Amiga key   RG = right Amiga key
  47. LA = left ALT key     RA = right ALT key
  48. LS = left SHIFT key   RS = right SHIFT key
  49. CT = ConTRoL key
  50. CO = command key
  51.  
  52. Format of the imessage->Qualifier variable (necessary bits only):
  53.  
  54.                                 RG  LG  RA  LA  CT      RS  LS  <-- keys
  55. 15  14  13  12  11  10  09  08  07  06  05  04  03  02  01  00  <-- bits
  56.  
  57.  0   0   0   0   0   0   0   0   1   1   1   1   1   0   0   0  <-- mask
  58.                |               |               |                <-- bytes
  59.        0               0               F               8        <-- hex
  60.  <------- 8 bit positions -------<                              <-- shift left
  61. RG  LG  RA  LA  CT                                              <-- new pos
  62.                               and
  63.  0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1  <-- mask
  64.                |               |               |                <-- bytes
  65.        0               0               0               3        <-- hex
  66.                      <--------- 9 bit positions ---------<      <-- shift left
  67.                     RS  LS                                      <-- new pos
  68.  
  69. Format of the "key" variable returned by sfInputKey() function:
  70.  
  71. RG  LG  RA  LA  CT  RS  LS  CO  (-------- key value ---------)
  72. 15  14  13  12  11  10  09  08  07  06  05  04  03  02  01  00  <-- bits
  73.  
  74. where key value equals:
  75.   if CO (command) bit set (== 1):
  76.     command key value as defined in input.h
  77.   if CO (command) bit cleared (== 0):
  78.     ASCII value of key pressed
  79.  
  80. */
  81.  
  82.   /* determine key qualifier as shown above */
  83.   qual = ((QUAL & 0xf8) << 8) | ((QUAL & 0x03) << 9);
  84.  
  85.   /* if control key, ignore this fact so that RawKeyConvert() will process
  86.    * (remember that the console device processes some control key sequences
  87.    * as other keys, for example, ^M equals RETURN)
  88.    */
  89.   if (QUAL & IEQUALIFIER_CONTROL)
  90.     QUAL &= ~IEQUALIFIER_CONTROL;
  91.  
  92.   /* used for converting dead keys */
  93.   ievent.ie_position.ie_addr = *((APTR *)imessage->IAddress);
  94.   /* convert rawkey */
  95.   codes = RawKeyConvert( &ievent, (char *)buffer, SIZE, NULL );
  96.  
  97.   /* if any codes returned (i.e., down transition and not a dead key) */
  98.   if (codes)
  99.   {
  100.     switch (buffer[0])
  101.     {
  102.       /* single code keys */
  103.       case 0x1b: key = COMMAND_ESCAPE; break;
  104.       case 0x08: key = COMMAND_BACKSPACE; break;
  105.       case 0x7f: key = COMMAND_DELETE; break;
  106.       case 0x09: key = COMMAND_TAB; break;
  107.       case 0x0d: key = COMMAND_RETURN; break;
  108.       case 0x9b:
  109.         if (codes > 1)
  110.         {
  111.           /* "escape" code keys */
  112.           switch (buffer[1])
  113.           {
  114.             case 0x5a: key = COMMAND_TAB; break;  /* shift-TAB */
  115.             case 0x3f: key = COMMAND_HELP; break;
  116.             case 0x54:
  117.             case 0x41: key = COMMAND_UP_ARROW; break;
  118.             case 0x53:
  119.             case 0x42: key = COMMAND_DOWN_ARROW; break;
  120.             case 0x43: key = COMMAND_RIGHT_ARROW; break;
  121.             case 0x44: key = COMMAND_LEFT_ARROW; break;
  122.             case 0x20:
  123.               switch (buffer[2])
  124.               {
  125.                 /* shifted-arrow keys */
  126.                 case 0x40: key = COMMAND_RIGHT_ARROW; break;
  127.                 case 0x41: key = COMMAND_LEFT_ARROW; break;
  128.               }
  129.               break;
  130.             default:
  131.               /* function keys F1-F10 */
  132.               if (buffer[2] == 0x7e)
  133.                 key = COMMAND_F1 + (int)buffer[1]-0x30;
  134.               else if (buffer[3] == 0x7e)
  135.                 key = COMMAND_F1 + (int)buffer[2]-0x30;
  136.               break;
  137.           }
  138.         }
  139.         else
  140.           key = COMMAND_ESCAPE;
  141.         break;
  142.     }
  143.     /* if key!=0, a command key was pressed */
  144.     if (key)
  145.       key += QUAL_COMMAND;
  146.     /* else no command key, must've been ASCII character */
  147.     else
  148.       key = buffer[0];
  149.   }
  150.   /* attach qualifiers to key */
  151.   if (key)
  152.     key += qual;
  153.  
  154.   return (key);
  155. }
  156.