home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap13 / showcode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-07  |  1.4 KB  |  47 lines

  1. /*  showcode.c -- shows ASCII and scan codes for    */
  2. /*                keystrokes                        */
  3. /* Note: Set Screen Swapping On in the Debug menu.  */
  4. #include <stdio.h>
  5. #include <dos.h>
  6. #define KEYINTR 0x16  /* keyboard read interrupt */
  7. #define GETCHAR 0     /* read scancode function  */
  8. #define ESC '\033'    /* escape key              */
  9. struct SCANCODE {
  10.                 unsigned char ascii;  /* ascii code */
  11.                 unsigned char scan;   /* scan code  */
  12.                 };
  13. struct SCANCODE Readkey();
  14.  
  15. main()
  16. {
  17.     struct SCANCODE keys;
  18.  
  19.     printf("Press keys to see their scancodes. ");
  20.     printf("Press the Esc key to quit.\n");
  21.     do  {
  22.         keys = Readkey();
  23.         if (keys.ascii > 0 && keys.ascii < 040)
  24.             printf("^%c: ascii = %3d, scan = %3d\n",
  25.                     keys.ascii + 0100, keys.ascii,
  26.                     keys.scan);
  27.         else if (keys.ascii >= 40)
  28.             printf(" %c: ascii = %3d, scan = %3d\n",
  29.                     keys.ascii, keys.ascii, keys.scan);
  30.         else
  31.             printf("  : ascii = %3d, scan = %3d\n",
  32.                     keys.ascii, keys.scan);
  33.         } while (keys.ascii != ESC);
  34. }
  35.  
  36. struct SCANCODE Readkey()
  37. {
  38.     union REGS reg;
  39.     struct SCANCODE scancode;
  40.  
  41.     reg.h.ah = GETCHAR;
  42.     int86(KEYINTR, ®, ®);
  43.     scancode.ascii = reg.h.al;
  44.     scancode.scan = reg.h.ah;
  45.     return (scancode);
  46. }
  47.