home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c040 / 1.ddi / SAMPLES / TOOLS.C$ / TOOLS.bin
Encoding:
Text File  |  1988-11-21  |  3.1 KB  |  100 lines

  1. /* TOOLS - Module containing several general functions that can be used
  2.  * from any program. Include TOOLS.H to use.
  3.  */
  4.  
  5. #include <conio.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <graph.h>
  9. #include <bios.h>
  10. #include "tools.h"
  11.  
  12. /* delay - Pauses for a specified number of microseconds. Used to slow
  13.  * life by delaying between generations.
  14.  *
  15.  * Params: wait - time in microseconds
  16.  *
  17.  * Return: None
  18.  */
  19. void delay( clock_t wait )
  20. {
  21.  
  22.     clock_t t1, t2;
  23.  
  24.     if( !wait )
  25.         return;
  26.  
  27.     t1 = wait + clock();
  28.     do
  29.     {
  30.         t2 = clock();
  31.     } while( t2 < t1 );
  32. }
  33.  
  34. /* getkey - Gets a key from the keyboard. This routine distinguishes
  35.  * between ASCII keys and function or control keys with different shift
  36.  * states. It also accepts a flag to return immediately if no key is
  37.  * available.
  38.  *
  39.  * Params: waitflag - Code to indicate how to handle keyboard buffer:
  40.  *   NO_WAIT     Return 0 if no key in buffer, else return key
  41.  *   WAIT        Return first key if available, else wait for key
  42.  *   CLEAR_WAIT  Throw away any key in buffer and wait for new key
  43.  *
  44.  * Return: One of the following:
  45.  *
  46.  *   Keytype                                High Byte    Low Byte
  47.  *   -------                                ---------    --------
  48.  *   No key available (only with NO_WAIT)       0           0
  49.  *   ASCII value                                0        ASCII code
  50.  *   Unshifted function or keypad               1        scan code
  51.  *   Shifted function or keypad                 2        scan code
  52.  *   CTRL function or keypad                    3        scan code
  53.  *   ALT function or keypad                     4        scan code
  54.  *
  55.  * Note:   getkey cannot return codes for keys not recognized by BIOS
  56.  *         int 16, such as the CTRL-UP or the 5 key on the numeric keypad.
  57.  */
  58. unsigned getkey( int waitflag )
  59. {
  60.     unsigned inkey, shiftstate;
  61.  
  62.     /* If CLEAR_WAIT, drain the keyboard buffer. */
  63.     if( waitflag == CLEAR_WAIT )
  64.         while( _bios_keybrd( _KEYBRD_READY ) )
  65.             _bios_keybrd( _KEYBRD_READ );
  66.  
  67.     /* If NO_WAIT, return 0 if there is no key ready. */
  68.     if( !waitflag && !_bios_keybrd( _KEYBRD_READY ) )
  69.         return FALSE;
  70.  
  71.     /* Get key code. */
  72.     inkey = _bios_keybrd( _KEYBRD_READ );
  73.  
  74.     /* If low byte is not zero, it's an ASCII key. Check scan code to see
  75.      * if it's on the numeric keypad. If not, clear high byte and return.
  76.      */
  77.     if( inkey & 0x00ff )
  78.         if( (inkey >> 8) < 69 )
  79.             return( inkey & 0x00ff );
  80.  
  81.     /* For function keys and numeric keypad, put scan code in low byte
  82.      * and shift state codes in high byte.
  83.      */
  84.     inkey >>= 8;
  85.     shiftstate = _bios_keybrd( _KEYBRD_SHIFTSTATUS ) & 0x000f;
  86.     switch( shiftstate )
  87.     {
  88.         case 0:
  89.             return( 0x0100 | inkey );  /* None (1)    */
  90.         case 1:
  91.         case 2:
  92.         case 3:
  93.             return( 0x0200 | inkey );  /* Shift (2)   */
  94.         case 4:
  95.             return( 0x0300 | inkey );  /* Control (3) */
  96.         case 8:
  97.             return( 0x0400 | inkey );  /* Alt (4)     */
  98.     }
  99. }
  100.