home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / KEYWATCH.C < prev    next >
C/C++ Source or Header  |  1991-12-30  |  5KB  |  155 lines

  1. /* keywatch.c 12-29-91 Robert Mashlan, Public Domain                *\
  2.  
  3.    DOS compiler portability modifications added by Bob Stout,
  4.    1:106/2000.6
  5.  
  6.    This program monitors the keyboard interrupt, and stores the
  7.    status of each key as to whether it is pressed or released.
  8.  
  9.    This is done by capturing interrupt 9, and watching the make/break
  10.    codes.  The status is updated in the keys array, where 1 means
  11.    that the key is pressed, while 0 means the key is released.  The
  12.    key array is uses the scan code for an index instead of the ascii
  13.    character.  It is simple enough to find the scan code for a key,
  14.    just run this program and watch the display.
  15.  
  16.    The ekeys array will reflect the status of keys found on an AT
  17.    keyboard.  For instance, the left and right alt keys are
  18.    differentiated, as well as the edit control keys on the numeric
  19.    keypad and the one not on the numeric keypad.
  20.  
  21.    Since this program installs an interrupt handler, it should be
  22.    terminated normally, such the keyboard handler can be removed.
  23.    The ^C/^Break exit is captured via signal(), but all possible
  24.    exits should be trapped.
  25.  
  26. \*                                                                  */
  27.  
  28. #include <stdio.h>
  29. #include <dos.h>
  30. #include <conio.h>
  31. #include <signal.h>
  32.  
  33. #if defined(__TURBOC__)
  34.  #define _interrupt interrupt
  35.  #define _far far
  36.  #define IN_PORT(port)           inportb(port)
  37.  #define IN_PORTW(port)          inport(port)
  38.  #define OUT_PORT(port, val)     outportb(port, val)
  39.  #define OUT_PORTW(port, val)    outport(port, val)
  40. #else
  41.  #if defined(__ZTC__)
  42.   #include <int.h>
  43.  #else /* MSC, QC, Watcom */
  44.   #define getvect(n) _dos_getvect(n)
  45.   #define setvect(n,v) _dos_setvect(n,v)
  46.  #endif
  47.  #define IN_PORT(port)           inp(port)
  48.  #define IN_PORTW(port)          inpw(port)
  49.  #define OUT_PORT(port, val)     outp(port, val)
  50.  #define OUT_PORTW(port, val)    outpw(port, val)
  51. #endif
  52.  
  53. volatile char keys[128];           /* array of key states           */
  54. volatile char ekeys[128];          /* array of AT key states        */
  55.  
  56. #define KEYPORT        0x60        /* keyboard scan code port       */
  57. #define keyport()      IN_PORT(KEYPORT)
  58.         /* macro that returns the scancode of the key that caused   */
  59.         /* the interrupt                                            */
  60.  
  61. /* Define:                                                          *\
  62.  
  63.    installisr()
  64.          installation macro, installs newkbisr() in the keyboard
  65.          interrupt chain
  66.  
  67.    removeisr()
  68.          removal macro, call to remove newkbisr() from interrupt
  69.          chain.  oldkbisr()  must be removed before program ends
  70. \*                                                                  */
  71.  
  72. #ifdef __ZTC__
  73.  #define installisr() int_intercept(0x09, newkbisr, 0)
  74.  #define removeisr()  int_restore(0x09);
  75. #else
  76.  #define installisr()  (oldkbisr=getvect(0x09),setvect(0x09,newkbisr))
  77.  #define removeisr()   setvect(0x09,oldkbisr)
  78.  #ifdef __TURBOC__
  79.   void _interrupt (_far *oldkbisr)(void);    /* address of old ISR   */
  80.  #else
  81.   void (_interrupt _far *oldkbisr)(void);    /* address of old ISR   */
  82.  #endif
  83. #endif
  84.  
  85. #ifdef __ZTC__
  86.  int newkbisr(struct INT_DATA *pd)
  87. #elif defined(__TURBOC__)
  88.  void _interrupt newkbisr(void)
  89. #else
  90.  void _interrupt _far newkbisr(void)
  91. #endif
  92. {
  93.       static extkey;
  94.       unsigned char scancode = keyport();      /* read keyboard scan code */
  95.  
  96.       if (scancode == 0xe0)
  97.             extkey = 1;               /* use ekey array on next scan code */
  98.       else
  99.       {
  100.             if (scancode & 0x80)                          /* key released */
  101.                   (extkey ? ekeys : keys)[scancode & 0x7f] = 0;
  102.             else  (extkey ? ekeys : keys)[scancode] = 1;
  103.             extkey = 0;
  104.       }
  105.  
  106. #ifdef __ZTC__
  107.       return 0;                         /* chain to previous keyboard ISR */
  108. #else
  109.       oldkbisr();                       /* chain to previous keyboard ISR */
  110. #endif
  111. }
  112.  
  113. int keyspressed(void)           /* returns number of keys being held down */
  114. {
  115.       int i, result = 0;
  116.  
  117.       for (i = 0; i < 128; i++)
  118.       {
  119.             result += keys[i];
  120.             result += ekeys[i];
  121.       }
  122.       return result;
  123. }
  124.  
  125. int main(void)
  126. {
  127.       int lastkeycount = 0;
  128.  
  129.       signal(SIGINT,SIG_IGN);  /* ingnore ^C and ^Break */
  130.       installisr();            /* install interrupt handler */
  131.       while(1)
  132.       {
  133.             int i;
  134.  
  135.             if (keyspressed() != lastkeycount) /* change in keystatus */
  136.             {
  137.                   lastkeycount = keyspressed();
  138.                   puts("---");
  139.                   for(i = 0; i < 128; i++)
  140.                   {
  141.                         if (keys[i])
  142.                               printf("key with scan code %02x "
  143.                                     "has been pressed\n", i);
  144.                         if (ekeys[i])
  145.                               printf("key with scan codes e0 %02x "
  146.                                     "had been pressed\n", i);
  147.                   }
  148.             }
  149.             if (kbhit() && getch()==0x1b) /* terminate when Esc pressed */
  150.                   break;
  151.       }
  152.       removeisr();   /* remove interrupt handler */
  153.       return 0;
  154. }
  155.