home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff291.lzh / Keyboard / Keyboard.c < prev    next >
C/C++ Source or Header  |  1989-12-12  |  10KB  |  432 lines

  1. /* Keyboard.c
  2.  
  3.    by Fabbian G. Dufoe, III
  4.    This is a public domain file.  You may use it any way you wish.
  5.  
  6.    The functions in this file take an Intuition RAWKEY class message and
  7.    return the value of the key which was pressed.  For standard ASCII
  8.    characters ReadKey() returns the character.  For function keys it returns
  9.    a zero and places a code representing the key in a buffer supplied by the
  10.    calling program.  The codes are defined in Keys.h.
  11. */
  12.  
  13. #include "Keyboard.h"
  14.  
  15.  
  16. struct Device *ConsoleDevice = NULL;
  17. static struct IOStdReq ConsoleMsg;
  18.  
  19.  
  20. void
  21. CloseReadConsole()
  22. /* FUNCTION
  23.       This function closes the Console Device opened by OpenReadConsole().
  24.       If the Console Device was not successfully opened CloseReadConsole()
  25.       will return without doing anything.
  26.  
  27.    INPUT
  28.       None
  29.  
  30.    RESULTS
  31.       None
  32. */
  33. {
  34.    if (ConsoleDevice != NULL)
  35.       CloseDevice(&ConsoleMsg);
  36. }
  37.  
  38.  
  39. #ifdef LATTICE
  40. int
  41. DeadKeyConvert(struct IntuiMessage *Message,
  42.                UBYTE *KeyBuffer,
  43.                int BufferSize,
  44.                struct KeyMap *KeyMap)
  45. #else
  46. int
  47. DeadKeyConvert(Message, KeyBuffer, BufferSize, KeyMap)
  48. struct IntuiMessage *Message;
  49. UBYTE *KeyBuffer;
  50. int BufferSize;
  51. struct KeyMap *KeyMap;
  52. #endif
  53. /* FUNCTION
  54.       This function converts an Intuition RAWKEY message to the kind of
  55.       keycodes returned by the Console Device.  It uses the Console Device's
  56.       RawKeyConvert() function.
  57.  
  58.    INPUT
  59.       Message - a pointer to the intuition message
  60.       KeyBuffer - a pointer to the buffer the user supplied for keycodes
  61.       BufferSize - the size of KeyBuffer
  62.       KeyMap - a pointer to a KeyMap structure to be used for the
  63.                conversion.  A NULL value selects the default KeyMap.
  64.  
  65.    RESULTS
  66.       The function returns -2 if the message was not a RAWKEY class
  67.       message.  If the number of keycodes produced was greater than
  68.       BufferSize the function returns -1.  Otherwise the function returns
  69.       the number of keycodes it placed in the buffer.
  70. */
  71. {
  72.    static struct InputEvent InputEvent =
  73.    {
  74.       NULL,             /* struct InputEvent *ie_NextEvent; */
  75.       IECLASS_RAWKEY,   /* UBYTE ie_Class; */
  76.       0,                /* UBYTE ie_SubClass; */
  77.       0,                /* UWORD ie_Code; */
  78.       0                 /* UWORD ie_Qualifier; */
  79.                         /* union */
  80.                         /* { */
  81.                         /*    struct */
  82.                         /*    { */
  83.                         /*       WORD ie_x; */
  84.                         /*       WORD ie_y; */
  85.                         /*    } ie_xy; */
  86.                         /*    APTR ie_addr; */
  87.                         /* } ie_position; */
  88.                         /* struct timeval ie_TimeStamp; */
  89.    };
  90.  
  91.    if (Message->Class != RAWKEY)
  92.       return(-2);
  93.    /* if (Message->Code & IECODE_UP_PREFIX)
  94.       return(0); */
  95.  
  96.    InputEvent.ie_Code = Message->Code;
  97.    InputEvent.ie_Qualifier = Message->Qualifier;
  98.    InputEvent.ie_position.ie_addr = (APTR)*(Message->IAddress);
  99.  
  100.    return(RawKeyConvert(&InputEvent, KeyBuffer, BufferSize, KeyMap));
  101. }
  102.  
  103.  
  104. int
  105. OpenReadConsole()
  106. /* FUNCTION
  107.       This function gets a pointer to the Console Device by opening a
  108.       Console Device without attaching it to any window.  Its purpose is to
  109.       make the Console Device function RawKeyConvert() available.
  110.  
  111.    INPUT
  112.       None
  113.  
  114.    RESULTS
  115.       The function returns 0 if it succeeds, -1 if it failed to open the
  116.       Console Device.
  117. */
  118. {
  119.    if (OpenDevice("console.device", -1L, &ConsoleMsg, 0) != 0)
  120.       return(-1);
  121.    ConsoleDevice = (struct Device *)ConsoleMsg.io_Device;
  122.    return(0);
  123. }
  124.  
  125.  
  126. #ifdef LATTICE
  127. char
  128. ReadKey(struct IntuiMessage *Message,
  129.         unsigned short int *KeyID,
  130.         struct KeyMap *KeyMap)
  131. #else
  132. char
  133. ReadKey(Message, KeyID, KeyMap)
  134. struct IntuiMessage *Message;
  135. unsigned short int *KeyID;
  136. struct KeyMap *KeyMap;
  137. #endif
  138. /* FUNCTION
  139.       This function converts an Intuition RAWKEY message to an ASCII
  140.       character or an integer code identifying the special key pressed.
  141.  
  142.    INPUT
  143.       Message - a pointer to the Intuition message
  144.       KeyID - a pointer used to return the ID code of a function key
  145.       KeyMap - a pointer to the keymap structure to be used for the
  146.                conversion.  A NULL pointer specifies the default keymap.
  147.  
  148.    RETURNS
  149.       If the function converts a RAWKEY message to an ASCII character it
  150.       returns that character.  It returns zero if a special key was pressed
  151.       and it places the key's ID code in the integer pointed to by KeyID.
  152.       If the message was not a RAWKEY class message or if it was a "key up"
  153.       message ReadKey() returns -2.  The calling program can ignore any
  154.       calls which return -2.  If it fails it returns -1.
  155. */
  156. {
  157.    int actual;
  158.    UBYTE KeyBuffer[10];
  159.  
  160.    *KeyID = 0;
  161.    if (Message->Class != RAWKEY)
  162.       return(-2);
  163.       /* If it's not a RAWKEY message we'll just ignore it.  We tell the
  164.          caller it can ignore it, too. */
  165.    if (Message->Code & IECODE_UP_PREFIX)
  166.       return(-2);
  167.       /* If it's a key up message we'll ignore it and tell the caller to
  168.          ignore it, too. */
  169.    actual = DeadKeyConvert(Message, KeyBuffer, sizeof(KeyBuffer), KeyMap);
  170.    if (actual == 1)
  171.       return((char)KeyBuffer[0]);
  172.       /* If DeadKeyConvert() converted the message to a single code we can
  173.          return it to the caller. */
  174.    switch (KeyBuffer[0])
  175.    {
  176.    case 0x9b:
  177.       switch (KeyBuffer[1])
  178.       {
  179.       case ' ':
  180.          switch (KeyBuffer[2])
  181.          {
  182.          case '@':
  183.             *KeyID = K_S_RIGHT;
  184.             break;
  185.          case 'A':
  186.             *KeyID = K_S_LEFT;
  187.             break;
  188.          default:
  189.             break;
  190.          }
  191.          break;
  192.       case '?':
  193.          switch (KeyBuffer[2])
  194.          {
  195.          case '~':
  196.             *KeyID = K_HELP;
  197.             break;
  198.          default:
  199.             break;
  200.          }
  201.          break;
  202.       case '0':
  203.          switch (KeyBuffer[2])
  204.          {
  205.          case '~':
  206.             *KeyID = K_F1;
  207.             break;
  208.          default:
  209.             break;
  210.          }
  211.          break;
  212.       case '1':
  213.          switch (KeyBuffer[2])
  214.          {
  215.          case '~':
  216.             *KeyID = K_F2;
  217.             break;
  218.          case '0':
  219.             switch (KeyBuffer[3])
  220.             {
  221.             case '~':
  222.                *KeyID = K_S_F1;
  223.                break;
  224.             default:
  225.                break;
  226.             }
  227.             break;
  228.          case '1':
  229.             switch (KeyBuffer[3])
  230.             {
  231.             case '~':
  232.                *KeyID = K_S_F2;
  233.                break;
  234.             default:
  235.                break;
  236.             }
  237.             break;
  238.          case '2':
  239.             switch (KeyBuffer[3])
  240.             {
  241.             case '~':
  242.                *KeyID = K_S_F3;
  243.                break;
  244.             default:
  245.                break;
  246.             }
  247.             break;
  248.          case '3':
  249.             switch (KeyBuffer[3])
  250.             {
  251.             case '~':
  252.                *KeyID = K_S_F4;
  253.                break;
  254.             default:
  255.                break;
  256.             }
  257.             break;
  258.          case '4':
  259.             switch (KeyBuffer[3])
  260.             {
  261.             case '~':
  262.                *KeyID = K_S_F5;
  263.                break;
  264.             default:
  265.                break;
  266.             }
  267.             break;
  268.          case '5':
  269.             switch (KeyBuffer[3])
  270.             {
  271.             case '~':
  272.                *KeyID = K_S_F6;
  273.                break;
  274.             default:
  275.                break;
  276.             }
  277.             break;
  278.          case '6':
  279.             switch (KeyBuffer[3])
  280.             {
  281.             case '~':
  282.                *KeyID = K_S_F7;
  283.                break;
  284.             default:
  285.                break;
  286.             }
  287.             break;
  288.          case '7':
  289.             switch (KeyBuffer[3])
  290.             {
  291.             case '~':
  292.                *KeyID = K_S_F8;
  293.                break;
  294.             default:
  295.                break;
  296.             }
  297.             break;
  298.          case '8':
  299.             switch (KeyBuffer[3])
  300.             {
  301.             case '~':
  302.                *KeyID = K_S_F9;
  303.                break;
  304.             default:
  305.                break;
  306.             }
  307.             break;
  308.          case '9':
  309.             switch (KeyBuffer[3])
  310.             {
  311.             case '~':
  312.                *KeyID = K_S_F10;
  313.                break;
  314.             default:
  315.                break;
  316.             }
  317.             break;
  318.          default:
  319.             break;
  320.          }
  321.          break;
  322.       case '2':
  323.          switch (KeyBuffer[2])
  324.          {
  325.          case '~':
  326.             *KeyID = K_F3;
  327.             break;
  328.          default:
  329.             break;
  330.          }
  331.          break;
  332.       case '3':
  333.          switch (KeyBuffer[2])
  334.          {
  335.          case '~':
  336.             *KeyID = K_F4;
  337.             break;
  338.          default:
  339.             break;
  340.          }
  341.          break;
  342.       case '4':
  343.          switch (KeyBuffer[2])
  344.          {
  345.          case '~':
  346.             *KeyID = K_F5;
  347.             break;
  348.          default:
  349.             break;
  350.          }
  351.          break;
  352.       case '5':
  353.          switch (KeyBuffer[2])
  354.          {
  355.          case '~':
  356.             *KeyID = K_F6;
  357.             break;
  358.          default:
  359.             break;
  360.          }
  361.          break;
  362.       case '6':
  363.          switch (KeyBuffer[2])
  364.          {
  365.          case '~':
  366.             *KeyID = K_F7;
  367.             break;
  368.          default:
  369.             break;
  370.          }
  371.          break;
  372.       case '7':
  373.          switch (KeyBuffer[2])
  374.          {
  375.          case '~':
  376.             *KeyID = K_F8;
  377.             break;
  378.          default:
  379.             break;
  380.          }
  381.          break;
  382.       case '8':
  383.          switch (KeyBuffer[2])
  384.          {
  385.          case '~':
  386.             *KeyID = K_F9;
  387.             break;
  388.          default:
  389.             break;
  390.          }
  391.          break;
  392.       case '9':
  393.          switch (KeyBuffer[2])
  394.          {
  395.          case '~':
  396.             *KeyID = K_F10;
  397.             break;
  398.          default:
  399.             break;
  400.          }
  401.          break;
  402.       case 'A':
  403.          *KeyID = K_UP;
  404.          break;
  405.       case 'B':
  406.          *KeyID = K_DOWN;
  407.          break;
  408.       case 'C':
  409.          *KeyID = K_RIGHT;
  410.          break;
  411.       case 'D':
  412.          *KeyID = K_LEFT;
  413.          break;
  414.       case 'S':
  415.          *KeyID = K_S_DOWN;
  416.          break;
  417.       case 'T':
  418.          *KeyID = K_S_UP;
  419.          break;
  420.       default:
  421.          break;
  422.       }
  423.       break;
  424.    default:
  425.       break;
  426.    }
  427.    if (*KeyID == 0)
  428.       return(-1);
  429.    else
  430.       return(0);
  431. }
  432.