home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / windbase / memslc.3 / queue / getkey.c next >
C/C++ Source or Header  |  1994-12-17  |  11KB  |  277 lines

  1. /*****************************************************************************\
  2. **                                                                           **
  3. **  WW     WW IIIIIIII NNN   NN DDDDDDD  BBBBBBB     AA     SSSSSS EEEEEEEE  **
  4. **  WW  W  WW    II    NNNN  NN DD    DD BB    BB  AA  AA  SS      EE        **
  5. **  WW  W  WW    II    NN NN NN DD    DD BBBBBBB  AAAAAAAA  SSSSSS EEEEEE    **
  6. **   WW W WW     II    NN  NNNN DD    DD BB    BB AA    AA      SS EE        **
  7. **    WWWWW   IIIIIIII NN   NNN DDDDDDD  BBBBBBB  AA    AA SSSSSS  EEEEEEEE  **
  8. **                                                                           **
  9. **   SSSSSS  OOOOOO  FFFFFFFF TTTTTTTT WW     WW    AA    RRRRRRR  EEEEEEEE  **
  10. **  SS      OO    OO FF          TT    WW  W  WW  AA  AA  RR    RR EE        **
  11. **   SSSSS  OO    OO FFFFF       TT    WW  W  WW AAAAAAAA RRRRRRR  EEEEEE    **
  12. **       SS OO    OO FF          TT     WW W WW  AA    AA RR   RR  EE        **
  13. **  SSSSSS   OOOOOO  FF          TT      WWWWW   AA    AA RR    RR EEEEEEEE  **
  14. **                                                                           **
  15. *********** NOTICE ************************************************************
  16. **        This file contains valuable trade secrets and proprietary          **
  17. **        assets of Windbase Software Inc.  Embodying substantial            **
  18. **        creative efforts and confidential information.  Unauthorized       **
  19. **        use, copying, decompiling, translating, disclosure or              **
  20. **        transfer, of any kind, is strictly prohibited.                     **
  21. **                                                                           **
  22. **        COPYRIGHT (C) 1992, 1993, 1994.  Windbase Software Inc.            **
  23. **        ALL RIGHTS RESERVED.                                               **
  24. \*****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <conio.h>
  28. #include <string.h>
  29. #include <dos.h>
  30. #ifdef MSC
  31. #  include <sys/types.h>
  32. #  include <sys/timeb.h>
  33. #endif
  34.  
  35. #include "../../memsl.h"
  36. #include "getkey.h"
  37.  
  38. /*************************************************************************\
  39. **  KeyStat() is used to retrieve the current keyboard control key status.
  40. **  The check is made with the low level DOS call 0x16 (Keyboard services)
  41. **  with 'ah' set to 2.  The DOS call can also be used to check the status
  42. **  of other control keys.  Since all <CTRL> and <ALT> key combinations
  43. **  generate a key sequence under MS-DOS, we are only interested in the
  44. **  <SHIFT> key.
  45. \*************************************************************************/
  46.  
  47. #ifdef WBSTDC
  48.   void KeyStat(struct key_stat *keystat)
  49. #else
  50.   void KeyStat(keystat)
  51.     struct key_stat *keystat;
  52. #endif
  53.   {
  54.     union REGS regs;
  55.  
  56.     memset(keystat,0,sizeof(struct key_stat));
  57.     memset(®s,0,sizeof(union REGS));
  58.  
  59.     regs.h.ah = 02;
  60.  
  61.     int86(0x16,®s,®s);
  62.  
  63.     if ((regs.h.al&0x80) == 0x80)
  64.       keystat->insert = 1;
  65.     if ((regs.h.al&0x01) == 0x01)
  66.       keystat->shiftkey = 1;
  67.     if ((regs.h.al&0x02) == 0x02)
  68.       keystat->shiftkey = 1;
  69.   }
  70.  
  71. #ifdef MSC
  72.  
  73. void delay(int milltime)
  74.   {
  75.     unsigned long time1, time2;
  76.     struct timeb timeb;
  77.  
  78.     ftime(&timeb);
  79.     time1 = (unsigned long) (timeb.time*1000) + (unsigned long) timeb.millitm;
  80.     time2 = (unsigned long) (timeb.time*1000) + (unsigned long) timeb.millitm
  81.                        + (unsigned long) milltime;
  82.  
  83.     while (time1 < time2)
  84.       {
  85.     ftime(&timeb);
  86.     time1 = (unsigned long) (timeb.time*1000)
  87.           + (unsigned long) timeb.millitm;
  88.       }
  89.   }
  90.  
  91. #endif
  92.  
  93. /*************************************************************************\
  94. **  GetKey() is the main keyboard processor.  GetKey() loops continuously
  95. **  until all keys are gathered from the keyboard and the keyboard is
  96. **  idle.  When the keyboard is idle GetKey() will return a character with
  97. **  no delay.  GetKey() translates the keypresses into the key codes as
  98. **  as defined in getkey.h.  If the queue is full additional key presses
  99. **  are ignored.
  100. \*************************************************************************/
  101.  
  102. #ifdef WBSTDC
  103.   int GetKey(WBQUEUE *keybuff)
  104. #else
  105.   int GetKey(keybuff)
  106.     WBQUEUE *keybuff;
  107. #endif
  108.   {
  109.     int ch;
  110.     struct key_stat keystat;
  111.  
  112.       /*
  113.       ** If the queue is full the key press is ignored for the moment.
  114.       ** If a key has been pressed and the queue is not full, process
  115.       ** the key.  If no keys were pressed and the queue is empty,
  116.       ** then there are no keys to return and GetKey() simply waits
  117.       ** for another key to be pressed.
  118.       */
  119.     while (WBQueueNumItems(keybuff) < WBQueueMaxItems(keybuff)
  120.       && (kbhit() || WBQueueNumItems(keybuff) == 0))
  121.       {
  122.         switch(ch = getch())
  123.           {
  124.             case 0:
  125.                 /*
  126.                 ** DOS returns zero along with a key code for special keys.
  127.                 ** For example, if the <F1> key is pressed, zero and ';'
  128.                 ** will be sitting in the key pool.
  129.                 */
  130.               switch(ch = getch())
  131.                 {
  132.                   case 59: ch = KEY_F1; break;
  133.                   case 60: ch = KEY_F2; break;
  134.                   case 61: ch = KEY_F3; break;
  135.                   case 62: ch = KEY_F4; break;
  136.                   case 63: ch = KEY_F5; break;
  137.                   case 64: ch = KEY_F6; break;
  138.                   case 65: ch = KEY_F7; break;
  139.                   case 66: ch = KEY_F8; break;
  140.                   case 67: ch = KEY_F9; break;
  141.                   case 68: ch = KEY_F10; break;
  142.                   case 84: ch = KEY_SF1; break;
  143.                   case 85: ch = KEY_SF2; break;
  144.                   case 86: ch = KEY_SF3; break;
  145.                   case 87: ch = KEY_SF4; break;
  146.                   case 88: ch = KEY_SF5; break;
  147.                   case 89: ch = KEY_SF6; break;
  148.                   case 90: ch = KEY_SF7; break;
  149.                   case 91: ch = KEY_SF8; break;
  150.                   case 92: ch = KEY_SF9; break;
  151.                   case 93: ch = KEY_SF10; break;
  152.                   case 94: ch = KEY_CF1; break;
  153.                   case 95: ch = KEY_CF2; break;
  154.                   case 96: ch = KEY_CF3; break;
  155.                   case 97: ch = KEY_CF4; break;
  156.                   case 98: ch = KEY_CF5; break;
  157.                   case 99: ch = KEY_CF6; break;
  158.                   case 100: ch = KEY_CF7; break;
  159.                   case 101: ch = KEY_CF8; break;
  160.                   case 102: ch = KEY_CF9; break;
  161.                   case 103: ch = KEY_CF10; break;
  162.                   case 104: ch = KEY_AF1; break;
  163.                   case 105: ch = KEY_AF2; break;
  164.                   case 106: ch = KEY_AF3; break;
  165.                   case 107: ch = KEY_AF4; break;
  166.                   case 108: ch = KEY_AF5; break;
  167.                   case 109: ch = KEY_AF6; break;
  168.                   case 110: ch = KEY_AF7; break;
  169.                   case 111: ch = KEY_AF8; break;
  170.                   case 112: ch = KEY_AF9; break;
  171.                   case 113: ch = KEY_AF10; break;
  172.                   case 71:
  173.                     KeyStat(&keystat);
  174.                     if (keystat.shiftkey)
  175.                       ch = KEY_SHOME;
  176.                     else
  177.                       ch = KEY_HOME;
  178.                   break;
  179.                   case 72:
  180.                     KeyStat(&keystat);
  181.                     if (keystat.shiftkey)
  182.                       ch = KEY_SUP;
  183.                     else
  184.                       ch = KEY_UP;
  185.                   break;
  186.                   case 73:
  187.                     KeyStat(&keystat);
  188.                     if (keystat.shiftkey)
  189.                       ch = KEY_SPGUP;
  190.                     else
  191.                       ch = KEY_PGUP;
  192.                   break;
  193.                   case 75:
  194.                     KeyStat(&keystat);
  195.                     if (keystat.shiftkey)
  196.                       ch = KEY_SLEFT;
  197.                     else
  198.                       ch = KEY_LEFT;
  199.                   break;
  200.                   case 77:
  201.