home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / GETKEY.ZIP / GETKEY.PAS
Encoding:
Pascal/Delphi Source File  |  1988-04-25  |  3.0 KB  |  56 lines

  1.  
  2.   (************************************************************************)
  3.   (*  For TP version 4:  Often I miss the old Read(kbd,ch), but don't     *)
  4.   (*  want to use the CRT unit just to get ReadKey.  This little inline   *)
  5.   (*  function allows you to do that without using the CRT unit, or the   *)
  6.   (*  Turbo3 unit to put back Kbd.  It is used like this: ch := GetKey;   *)
  7.   (*  It is a little different than ReadKey.  It flushes the keyboard     *)
  8.   (*  buffer first, in case you had accidentally pressed a key before     *)
  9.   (*  it was called.  Then it looks for a key, and waits until one is     *)
  10.   (*  pressed.  If the key code it detects is a byte with the value of    *)
  11.   (*  zero, that means the key is a function key or other special         *)
  12.   (*  key, and there is another byte waiting, so GetKey gets the second   *)
  13.   (*  byte and returns that to the calling routine.  In this case the     *)
  14.   (*  value returned is the scan key of the key pressed.  If there is     *)
  15.   (*  no first byte of zero GetKey returns whatever byte is in the        *)
  16.   (*  buffer, which is the ordinal of the character representing the      *)
  17.   (*  pressed.  As an ordinal function such as a character function       *)
  18.   (*  actually returns it value in the AL register to the calling         *)
  19.   (*  routine, the ReadKey := C is not really necessary, but is included  *)
  20.   (*  for clarity.  Hope this is of some value to you. It adds less than  *)
  21.   (*  50 bytes to your program if called once.                            *)
  22.   (*                                                                      *)
  23.   (*                 Rick Housh   CIS PIN 72466,212                       *)
  24.   (************************************************************************)
  25.  
  26.   Program GetKeyDemo;
  27.     var ch : char;
  28.  
  29.   Function GetKey : char;
  30.     var C :  char;
  31.     Begin
  32.       Inline(
  33.                      {; Function GetKey : Char}
  34.                      {; Clears the keyboard buffer then waits until}
  35.                      {; a key is struck.  If the key is a special, e.g.}
  36.                      {; function key, goes back and reads the next}
  37.                      {; byte in the keyboard buffer.  Thus does}
  38.                      {; nothing special with function keys.}
  39.       $B4/$0C        {       MOV  AH,$0C      ;Set up to clear buffer}
  40.       /$B0/$08       {       MOV  AL,8        ;then to get a char}
  41.       /$CD/$21       {SPCL:  INT  $21         ;Call DOS}
  42.       /$3C/$00       {       CMP  AL,0        ;If it's a 0 byte then}
  43.       /$75/$04       {       JNZ  CHRDY       ;is spec. key, get second byte}
  44.       /$B4/$08       {       MOV  AH,8        ;else set up for another}
  45.       /$EB/$F6       {       JMP  SHORT SPCL  ;and get it}
  46.       /$88/$46/<C    {CHRDY: MOV  <C[BP],AL  ;else put into function return}
  47.        );
  48.       GetKey := C;
  49.     end; {Inline function GetKey}
  50.  
  51.   Begin {Program GetKeyDemo}
  52.     WriteLn;
  53.     Write('Press a key: ');
  54.     ch := GetKey;
  55.     WriteLn(ch);
  56.   end.