home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FASKBHIT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  123 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  by David Goodenough & Bob Stout
  5. **
  6. **  Revisions:
  7. **  30-Mar-96  Ed Blackman  OS/2 mods
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <dos.h>
  12. #include "snipkbio.h"
  13. #include "extkword.h"
  14. #include "ext_keys.h"
  15. #include "mk_fp.h"
  16.  
  17. #if defined(__OS2__)
  18.  
  19. /* 30-Mar-96 - EBB:  it really isn't good to poll the keyboard in OS/2.
  20. ** A better design would be to call KbdCharIn(..., IO_WAIT, ...) in a
  21. ** separate thread, and sending a message via some form of IPC to the main
  22. ** thread when a key is available.  This code is intended to be more of an
  23. ** example of how to use the API, rather than something you would copy into
  24. ** production code.
  25. */
  26.  
  27. int fast_kbhit_os2(void)
  28. {
  29.     extern KBDKEYINFO ki;         /* defined in ISSHIFT.C */
  30.  
  31.     KbdPeek(&ki, 0);              /* peek in the keyboard buffer */
  32.     DosSleep(1);                  /* give up the rest of the time slice */
  33.  
  34.     return (ki.fbStatus & (1 << 6));  /* only return true if key is 'final' */
  35. }
  36.  
  37. void fast_kbflush_os2(void)
  38. {
  39.     KbdFlushBuffer(0);
  40. }
  41.  
  42. #else   /* assume DOS */
  43.  #define biosseg 0x40
  44.  
  45.  #define HEAD (*((unsigned short FAR *)MK_FP(biosseg, 0x1a)))
  46.  #define TAIL (*((unsigned short FAR *)MK_FP(biosseg, 0x1c)))
  47.  
  48. /*
  49. **  Detect a pending keypress
  50. */
  51.  
  52. int fast_kbhit_dos(void)
  53. {
  54.       int retval;
  55.  
  56.       disable();
  57.       retval = HEAD - TAIL;
  58.       enable();
  59.       return retval;
  60. }
  61.  
  62. /*
  63. **  Clear the keyboard buffer
  64. */
  65.  
  66. void fast_kbflush_dos(void)
  67. {
  68.       disable();
  69.       HEAD = TAIL;
  70.       enable();
  71. }
  72. #endif  /* !__OS2__ */
  73.  
  74. /*
  75. **  Enhanced work-alike for BASIC's INKEY$ function
  76. */
  77.  
  78. int ext_inkey(void)
  79. {
  80.       if (!fast_kbhit())
  81.             return EOF;
  82.       else  return ext_getch();
  83. }
  84.  
  85. #ifdef TEST
  86.  
  87. #include <conio.h>
  88. #include <time.h>
  89.  
  90. main()
  91. {
  92.       clock_t Wait;
  93.       int key;
  94.  
  95.       puts("Hit some keys while I kill some time...");
  96.       Wait = clock();
  97.       while (2 > ((clock() - Wait) / CLK_TCK))
  98.             ;
  99.  
  100.       puts("OK, stop hitting keys while I flush the keyboard...");
  101.       Wait = clock();
  102.       while (2 > ((clock() - Wait) / CLK_TCK))
  103.             ;
  104.  
  105.       fast_kbflush();
  106.       puts("Optionally, hit some key you didn't hit before...");
  107.       Wait = clock();
  108.       while (2 > ((clock() - Wait) / CLK_TCK))
  109.             ;
  110.       if (EOF == (key = ext_inkey()))
  111.             puts("You didn't hit anything");
  112.       else  printf("You hit extended keycode 0x%04X\n", key);
  113.       
  114.       puts("OK, now hit some other key you didn't hit before...");
  115.  
  116.       while (!fast_kbhit())
  117.             ;
  118.       printf("You hit extended keycode 0x%04X\n", ext_getch());
  119.       return 0;
  120. }
  121.  
  122. #endif /* TEST */
  123.