home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modem / cvt100.zip / KEYIO.C < prev    next >
Text File  |  1988-08-01  |  15KB  |  395 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <bios.h>
  4.  
  5. #define ESC         0x1B          /* ASCII ESCape character */
  6. #define ESCAPE      0x11B         /* Keyboard ESCape scan code */
  7. #define DEL         0x7F          /* ASCII DELete character */
  8. #define BKSP        0xE08         /* Keyboard BacKSPace scan code */
  9. #define F1          0x3B00        /* Keyboard Function key 1 scan code */
  10. #define F2          0x3C00        /* Keyboard Function key 2 scan code */
  11. #define F3          0x3D00        /* Keyboard Function key 3 scan code */
  12. #define F4          0x3E00        /* Keyboard Function key 4 scan code */
  13. #define F5          0x3F00        /* Keyboard Function key 5 scan code */
  14. #define F6          0x4000        /* Keyboard Function key 6 scan code */
  15. #define F7          0x4100        /* Keyboard Function key 7 scan code */
  16. #define F8          0x4200        /* Keyboard Function key 8 scan code */
  17. #define F9          0x4300        /* Keyboard Function key 9 scan code */
  18. #define F10         0x4400        /* Keyboard Function key 10 scan code */
  19. #define UP          0x4800        /* Keyboard Up Arrow scan code */
  20. #define DOWN        0x5000        /* Keyboard Down Arrow scan code */
  21. #define LEFT        0x4B00        /* Keyboard Left Arrow scan code */
  22. #define RIGHT       0x4D00        /* Keyboard Right Arrow scan code */
  23. #define K7          0x4737        /* Keyboard Numeric 7 scan code */
  24. #define K8          0x4838        /* Keyboard Numeric 8 scan code */
  25. #define K9          0x4939        /* Keyboard Numeric 9 scan code */
  26. #define KDASH       0x372A        /* Keyboard Numeric Asteric scan code */
  27. #define K4          0x4B34        /* Keyboard Numeric 4 scan code */
  28. #define K5          0x4C35        /* Keyboard Numeric 5 scan code */
  29. #define K6          0x4D36        /* Keyboard Numeric 6 scan code */
  30. #define KCOMA       0x4A2D        /* Keyboard Numeric Dash(minus) scan code */
  31. #define K1          0x4F31        /* Keyboard Numeric 1 scan code */
  32. #define K2          0x5032        /* Keyboard Numeric 2 scan code */
  33. #define K3          0x5133        /* Keyboard Numeric 3 scan code */
  34. #define KENTR       0x4E2B        /* Keyboard Numeric + (plus) scan code */
  35. #define K0          0x5230        /* Keyboard Numeric 0 scan code */
  36. #define KDOT        0x532E        /* Keyboard Numeric Period scan code */
  37.  
  38. /*****************************************************************************/
  39. /* function prototypes                                                       */
  40.  
  41. void         Keyinit( void );     /* Initialize the keyboard system */
  42. int          ConChk( void );      /* Check the keyboard for keystrokes */
  43. unsigned int GetKey( void );      /* Retrieve a scan code from keyboard */
  44. int          DoKey( void );       /* Transmit a key sequence */
  45.  
  46. static void TransKey( unsigned ); /* Translate a keystroke to a sequence */
  47. static int TransNumKey(unsigned); /* Translate Numeric Keypad keystroke */
  48. static int TransApplKey(unsigned);/* Translate Application Keypad keystroke */
  49. static void SendBksp( void );     /* Transmit the backspace character */
  50. void   SetKeyPad( int );          /* Set the keypad to APPLICATION, NUMERIC */
  51. void   SetCursorKey( int );       /* Set the cursor key mode */
  52.  
  53.  
  54.  
  55.  
  56. /*****************************************************************************/
  57. /* Global variables                                                          */
  58.  
  59. unsigned char backspace;          /* Backspace/Delete translation flag */
  60. unsigned char keyclick;           /* Keyclick mode on/off */
  61. unsigned char applkeypad;         /* Current state of keypad */
  62.  
  63. void TTSetup( void );             /* Communications setup function */
  64. void VidSetup( void );            /* Video setup function */
  65. void KeySetup( void );            /* Keyboard setup function */
  66. void VTSetup( void );             /* VT emulation setup function */
  67. void FileSetup( void );           /* File system setup function */
  68.  
  69.  
  70. /*****************************************************************************/
  71. /* External variables                                                        */
  72.  
  73.  
  74. /*****************************************************************************/
  75. /* Local Static data                                                         */
  76.  
  77. static char cursorkey = '[';      /* Sequence character in cursor key */
  78. static union REGS regs;           /* Registers for int86 call */
  79.  
  80. /*****************************************************************************/
  81. /*****************************************************************************/
  82.  
  83.  
  84. /* K E Y I N I T -- Initialize the keyboard system */
  85.  
  86. void Keyinit() {
  87.  
  88.     delay(1);                     /* Initialize Turbo C delay function */
  89.     if (GetKeySetup() == 0) {     /* If no values were available from */
  90.         backspace = 0;            /* a setup file then provide defaults */
  91.         keyclick = 0;
  92.         applkeypad = 0;
  93.     }
  94. }
  95.  
  96.  
  97. /*  C O N C H K  --  Check if any key strokes are waiting, check hot keys */
  98.  
  99. ConChk()
  100. {
  101.     void (*setupfunct)();         /* Pointer to selected setup function */
  102.  
  103.     regs.h.ah = 0x1;              /* Use function 1 of interrupt 0x16 */
  104.     int86(0x16,®s,®s);      /* to check for waiting keystrokes */
  105.     if ( regs.x.flags & 0x40 )    /* If the zero flag is set then no keys */
  106.        return(0);
  107.     else {                        /* Else a key has been pressed */
  108.  
  109.        switch (regs.x.ax) {       /* Check to see if it was a hot key */
  110.  
  111.           case F5:
  112.              setupfunct = TTSetup;
  113.              break;
  114.           case F6:
  115.              setupfunct = VidSetup;
  116.              break;
  117.           case F7:
  118.              setupfunct = KeySetup;
  119.              break;
  120.           case F8:
  121.              setupfunct = VTSetup;
  122.              break;
  123.           case F9:
  124.              setupfunct = FileSetup;
  125.              break;
  126.           default:                /* If not a Setup key return 1 indicating */
  127.              return(1);              /* a keystroke is ready */
  128.        }
  129.        GetKey();                  /* Retrieve keystroke */
  130.        (*setupfunct)();           /* Call the selected setup function */
  131.        return(0);                 /* Return indicating no keystroke here yet */
  132.     }
  133. }
  134.  
  135.  
  136. /*  G E T K E Y  --  Return a keyboard scan code */
  137.  
  138. unsigned int GetKey() {
  139.     register unsigned int scancode;
  140.  
  141.     scancode = bioskey(0);        /* Get a keystroke, waits if none ready */
  142.  
  143.     if (keyclick) {               /* If keyclick flag is set */
  144.         sound(250);                  /* Turn on low frequency sound */
  145.         delay(5);                    /* Wait a short time period */
  146.         nosound();                   /* Turn off the sound */
  147.     }
  148.     return(scancode);             /* Return the retrieved scancode */
  149. }
  150.  
  151.  
  152.  
  153. /*  D O K E Y  --  Retrieve and interpret a keystroke */
  154.  
  155. DoKey() {
  156.     unsigned scancode;
  157.  
  158.     scancode = GetKey();          /* Get a keystroke, waits if none ready */
  159.  
  160.     if (scancode == F10)          /* Check for EXIT character */
  161.        return(-1);
  162.     else                          /* Else translate the pressed key */
  163.        TransKey(scancode);
  164.  
  165.     return(0);                    /* return success */
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /* T R A N S K E Y  -- translate a scancode into a keystroke sequence */
  174.  
  175. static void TransKey( unsigned key ) {
  176.  
  177.  
  178.     switch(key) {                 /* Evaluate this keyboard scan code */
  179.        case BKSP:                 /* Backspace pressed */
  180.             SendBksp();
  181.             break;
  182.        case F1:                   /* Function key 1 pressed */
  183.            ttoc( ESC );
  184.            ttoc( 'P' );
  185.            break;
  186.        case F2:                   /* Function key 2 pressed */
  187.            ttoc( ESC );
  188.            ttoc( 'Q' );
  189.            break;
  190.        case F3:                   /* Function key 3 pressed */
  191.            ttoc( ESC );
  192.            ttoc( 'R' );
  193.            break;
  194.        case F4:                   /* Function key 4 pressed */
  195.            ttoc( ESC );
  196.            ttoc( 'S' );
  197.            break;
  198.        case UP:                   /* Up Arrow pressed */
  199.            ttoc(ESC);
  200.            ttoc(cursorkey);
  201.            ttoc('A');
  202.            break;
  203.        case DOWN:                 /* Down Arrow pressed */
  204.            ttoc(ESC);
  205.            ttoc(cursorkey);
  206.            ttoc('B');
  207.            break;
  208.        case RIGHT:                /* Right Arrow pressed */
  209.            ttoc(ESC);
  210.            ttoc(cursorkey);
  211.            ttoc('C');
  212.            break;
  213.        case LEFT:                 /* Left Arrow pressed */
  214.            ttoc(ESC);
  215.            ttoc(cursorkey);
  216.            ttoc('D');
  217.            break;
  218.  
  219.        default:                   /* No translation yet, check numeric pad */
  220.            if ( (TransNumKey(key) == 0 ) && (TransApplKey(key) == 0) )
  221.               ttoc( (char)key );  /* Still no translation, transmit char */
  222.            break;
  223.     }
  224. }
  225.  
  226. /* T R A N S N U M K E Y  --  Try and translate key from the Numeric Keypad */
  227.  
  228. static TransNumKey(register unsigned key) {
  229.  
  230.    if (applkeypad != 0)           /* If keypad is not in NUMERIC mode */
  231.       return(0);                    /* then no translation here possible */
  232.  
  233.    switch ( key ) {
  234.       case K7:                    /* Numeric 7 pressed */
  235.          ttoc('7');
  236.          return(1);
  237.       case K8:                    /* Numeric 8 pressed */
  238.          ttoc('8');
  239.          return(1);
  240.       case K9:                    /* Numeric 9 pressed */
  241.          ttoc('9');
  242.          return(1);
  243.       case KDASH:                 /* Numeric Minus pressed */
  244.          ttoc('-');
  245.          return(1);
  246.       case K4:                    /* Numeric 4 pressed */
  247.          ttoc('4');
  248.          return(1);
  249.       case K5:                    /* Numeric 5 pressed */
  250.          ttoc('5');
  251.          return(1);
  252.       case K6:                    /* Numeric 6 pressed */
  253.          ttoc('6');
  254.          return(1);
  255.       case KCOMA:                 /* Numeric Comma pressed */
  256.          ttoc(',');
  257.          return(1);
  258.       case K1:                    /* Numeric 1 pressed */
  259.          ttoc('1');
  260.          return(1);
  261.       case K2:                    /* Numeric 2 pressed */
  262.          ttoc('2');
  263.          return(1);
  264.       case K3:                    /* Numeric 3 pressed */
  265.          ttoc('3');
  266.          return(1);
  267.       case K0:                    /* Numeric 0 pressed */
  268.          ttoc('0');
  269.          return(1);
  270.       case KDOT:                  /* Numeric Period pressed */
  271.          ttoc('.');
  272.          return(1);
  273.       case KENTR:                 /* Numeric Enter pressed */
  274.          ttoc(13);
  275.          return(1);
  276.       default:
  277.    }
  278.    return(0);                     /* No translation */
  279. }
  280.  
  281. /* T R A N S A P P L K E Y  --  Try and translate key from Application Keypad*/
  282.  
  283. static TransApplKey(register unsigned key) {
  284.  
  285.    if (applkeypad != 1)           /* If keypad is not APPLICATION mode */
  286.       return(0);                    /* then no translation here possible */
  287.  
  288.    switch ( key )  {
  289.        case K7:                   /* Application key 7 pressed*/
  290.            ttoc(ESC);
  291.            ttoc('O');
  292.            ttoc('w');
  293.            return(1);
  294.        case K8:                   /* Application key 8 pressed */
  295.            ttoc(ESC);
  296.            ttoc('O');
  297.            ttoc('x');
  298.            return(1);
  299.        case K9:                   /* Application key 9 pressed */
  300.            ttoc(ESC);
  301.            ttoc('O');
  302.            ttoc('y');
  303.            return(1);
  304.        case KDASH:                /* Application key minus pressed */
  305.            ttoc(ESC);
  306.            ttoc('O');
  307.            ttoc('m');
  308.            return(1);
  309.        case K4:                   /* Application key 4 pressed */
  310.            ttoc(ESC);
  311.            ttoc('O');
  312.            ttoc('t');
  313.            return(1);
  314.        case K5:                   /* Application key 5 pressed */
  315.            ttoc(ESC);
  316.            ttoc('O');
  317.            ttoc('u');
  318.            return(1);
  319.        case K6:                   /* Application key 6 pressed */
  320.            ttoc(ESC);
  321.            ttoc('O');
  322.            ttoc('v');
  323.            return(1);
  324.        case KCOMA:                /* Application key Comma pressed */
  325.            ttoc(ESC);
  326.            ttoc('O');
  327.            ttoc('l');
  328.            return(1);
  329.        case K1:                   /* Application key 1 pressed */
  330.            ttoc(ESC);
  331.            ttoc('O');
  332.            ttoc('q');
  333.            return(1);
  334.        case K2:                   /* Application key 2 pressed */
  335.            ttoc(ESC);
  336.            ttoc('O');
  337.            ttoc('r');
  338.            return(1);
  339.        case K3:                   /* Application key 3 pressed */
  340.            ttoc(ESC);
  341.            ttoc('O');
  342.            ttoc('s');
  343.            return(1);
  344.        case K0:                   /* Application key 0 pressed */
  345.            ttoc(ESC);
  346.            ttoc('O');
  347.            ttoc('p');
  348.            return(1);
  349.        case KDOT:                 /* Application key Dot pressed */
  350.            ttoc(ESC);
  351.            ttoc('O');
  352.            ttoc('n');
  353.            return(1);
  354.        case KENTR:                /* Application key Enter pressed */
  355.            ttoc(ESC);
  356.            ttoc('O');
  357.            ttoc('M');
  358.            return(1);
  359.        default:
  360.    }
  361.    return(0);                     /* No translation */
  362. }
  363.  
  364.  
  365.  
  366. /* S E N D B K S P -- Send a backspace out */
  367.  
  368. static void SendBksp() {
  369.    if (backspace)                 /* If backspace flag is on then */
  370.       ttoc(8);                       /* transmit a backspace */
  371.    else
  372.       ttoc(DEL);                  /* Else transmit a delete */
  373. }
  374.  
  375. /* S E T K E Y P A D -- Set the keypad translation */
  376.  
  377. void SetKeyPad( int mode ) {
  378.  
  379.    if (mode)                      /* If a mode set is requested */
  380.       applkeypad = 1;               /* set the keypad to APPLICATION */
  381.    else                           /* Else */
  382.       applkeypad = 0;               /* set the keypad to NUMERIC mode*/
  383. }
  384.  
  385. /* S E T C U R S O R K E Y -- Set the cursior key mode */
  386.  
  387. void SetCursorKey( mode ) {       /* This establishes the second character */
  388.                                   /* of the cursor keys escape sequence */
  389.    if (mode)                      /* If cursor key mode is requested */
  390.       cursorkey = 'O';              /* then set cursor key to 'O' */
  391.    else                           /* Else */
  392.       cursorkey = '[';              /* use a bracket for escape sequences */
  393. }
  394.  
  395.