home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l351 / 1.ddi / EXAMPLES / KEYS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-26  |  2.0 KB  |  80 lines

  1. /* 
  2. KEYS.C -- demonstrate use of BIOS data area in 286|DOS-Extender
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8.  
  9. #ifdef DOSX286
  10. #include <phapi.h>
  11. #else
  12. typedef unsigned char UCHAR;
  13. typedef unsigned USHORT;
  14. typedef unsigned long ULONG;
  15. #endif
  16.  
  17. /*
  18.     Microsoft Visual C++ - 
  19.     We use based variables, which are perfect for mapped selectors
  20.     in 286|DOS-Extender, and much nicer to work with than either
  21.     the MAKEP() or MK_FP() macros.
  22. */
  23. _segment bios;
  24. #define TICKS()             (*((ULONG _based(bios) *) 0x6C))
  25. #define KB_STAT             (*((UCHAR _based(bios) *) 0x17))
  26. #define KB_HEAD             (*((USHORT _based(bios) *) 0x1A))
  27. #define KB_TAIL             (*((USHORT _based(bios) *) 0x1C))
  28.  
  29. #define KB_HIT()            (KB_HEAD != KB_TAIL)
  30. #define CTRL()              (KB_STAT & 4)
  31. #define ALT()               (KB_STAT & 8)
  32.  
  33. #define ESC                 0x1B
  34.  
  35. void fail(char *s, unsigned ret)
  36. {
  37.     printf("%s ret=%04X (%u)\n", s, ret, ret);
  38.     exit(ret);
  39. }
  40.  
  41. main()
  42. {
  43.     USHORT ret;
  44.     int done = 0;
  45.     int c;
  46.     
  47. #ifdef DOSX286
  48.     /*
  49.         Notice that DosGetBIOSSeg() is called only once, at the 
  50.         beginning of the program. We can then freely use the 
  51.         selector for as long as we want.
  52.     */
  53.     if ((ret = DosGetBIOSSeg((PSEL) &bios)) != 0)
  54.         fail("DosGetBIOSSeg - failure", ret);
  55. #else
  56.     bios = 0x40;
  57. #endif
  58.  
  59.     /* Note: Ctrl-Esc probably not a good choice: used by Windows too! */
  60.     puts("Press keys; Ctrl-Esc to quit");
  61.     while (! done)
  62.         if (KB_HIT())
  63.         {
  64.             c = getch();
  65.             printf("TIME=%-8lX ST=%02X CHAR=%02X ", TICKS(), KB_STAT, c);
  66.             if (c == 0)
  67.                 printf("%02X", getch());  /* extended character */
  68.             printf("\n");
  69.             if ((c == ESC) && CTRL()) /* Ctrl-ESC */ 
  70.                 break;
  71.     }
  72.     
  73. #ifdef DOSX286
  74.     /* Do _NOT_ DosFreeSeg(bios) ! */
  75. #endif
  76.  
  77.     puts("bye");
  78.     return 0;
  79. }
  80.