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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  ext_getch()
  5. **
  6. **  A getch() work-alike for use with extended keyboards.
  7. **
  8. **  Parameters: none
  9. **
  10. **  Returns: Extended key code as follows:
  11. **           0->255     Normal key
  12. **           256->511   Numeric pad key or Function key
  13. **           512->767   Cursor pad key or Numeric pad
  14. **                      "duplicate" key (Enter, /, *, -, +)
  15. **
  16. **  Original Copyright 1992 by Bob Stout as part of
  17. **  the MicroFirm Function Library (MFL)
  18. **
  19. **  The user is granted a free limited license to use this source file
  20. **  to create royalty-free programs, subject to the terms of the
  21. **  license restrictions specified in the LICENSE.MFL file.
  22. **
  23. **  Revisions:
  24. **  30-Mar-96  Ed Blackman  OS/2 mods
  25. */
  26.  
  27. #include <dos.h>
  28. #include <ctype.h>
  29. #include "hilobyte.h"
  30. #include "snipkbio.h"
  31. #include "ext_keys.h"
  32.  
  33. #define USING_DOS 0     /* Set to 1 to call DOS instead of the BIOS     */
  34.  
  35. int ext_getch(void)
  36. {
  37.       int key;
  38. #ifdef __OS2__
  39.       extern KBDKEYINFO ki;         /* defined in ISSHIFT.C */
  40.       KBDINFO kb_state;
  41.  
  42.       kb_state = setkbmode();       /* Change keyboard to binary mode */
  43.       KbdCharIn(&ki, IO_WAIT, 0);   /* Get the key */
  44.       restkbmode(kb_state);         /* restore previous keyboard mode */
  45.  
  46.       key = (ki.chScan << 8) + ki.chChar;       /* format it into an int */
  47. #else                       /* assume DOS */
  48.       union REGS regs;
  49.  
  50.  #if USING_DOS
  51.       regs.h.ah = 7;
  52.       intdos(®s, ®s);
  53.       key = regs.h.al;
  54.       if (0 == key)
  55.       {
  56.             regs.h.ah = 7;
  57.             intdos(®s, ®s);
  58.             key = (regs.h.al << 8);
  59.       }
  60.  #else
  61.       regs.h.ah = 0x10;
  62.       int86(0x16, ®s, ®s);
  63.       key = regs.x.ax;
  64.  #endif
  65.  
  66.       switch (LoByte(key))
  67.       {
  68.       case 0:
  69.             key = HiByte(key) + 256;
  70.             break;
  71.  
  72.       case 0xe0:
  73.             key = HiByte(key) + 512;
  74.             break;
  75.  
  76.       default:
  77.             if (0xe0 == HiByte(key))
  78.                   key = LoByte(key) + 512;
  79.             else
  80.             {
  81.                   if (ispunct(LoByte(key)) && HiByte(key) > 0x36)
  82.                         key = LoByte(key) + 512;
  83.                   else  key = LoByte(key);
  84.             }
  85.       }
  86. #endif
  87.       return key;
  88. }
  89.  
  90. int GetExtKey(int *isshift)
  91. {
  92.       int key =  ext_getch();
  93.  
  94.       *isshift = IsShift();
  95.       return key;
  96. }
  97.  
  98. #ifdef __OS2__
  99. KBDINFO setkbmode(void)
  100. {
  101.     USHORT rc;
  102.     KBDINFO kb_state;
  103.  
  104.     kb_state.cb = sizeof(kb_state);
  105.     KbdGetStatus(&kb_state, 0);
  106.     kb_state.fsMask &= ~KEYBOARD_ASCII_MODE;
  107.     kb_state.fsMask |= KEYBOARD_BINARY_MODE;
  108.     rc = KbdSetStatus(&kb_state, 0);
  109.  
  110. /*  if(rc) printf("KbdSetStatus rc = %04x\n", rc);  */
  111.  
  112.     return kb_state;
  113. }
  114.  
  115. void restkbmode(KBDINFO kb_state)    /* restore keyboard mode */
  116. {
  117.     USHORT rc;
  118.  
  119.     rc = KbdSetStatus(&kb_state, 0);
  120. }
  121. #endif      /* !__OS2__ */
  122.  
  123. #ifdef TEST
  124.  
  125. #include <stdio.h>
  126.  
  127. main()
  128. {
  129.       int key0, key1, shift;
  130.  
  131.       puts("Hit keys, Esc twice to stop\n");
  132.       for ( key0 = key1 = 0; !(key1 == Key_ESC && key0 == Key_ESC); )
  133.       {
  134.             key0 = key1;
  135.             key1 = GetExtKey(&shift);
  136.             printf("ext_getch() returned %0Xh, Shift is O%s\n",
  137.                   key1, shift ? "n" : "ff");
  138.       }
  139.       return 0;
  140. }
  141.  
  142. #endif /* TEST */
  143.