home *** CD-ROM | disk | FTP | other *** search
-
- (************************************************************************)
- (* For TP version 4: Often I miss the old Read(kbd,ch), but don't *)
- (* want to use the CRT unit just to get ReadKey. This little inline *)
- (* function allows you to do that without using the CRT unit, or the *)
- (* Turbo3 unit to put back Kbd. It is used like this: ch := GetKey; *)
- (* It is a little different than ReadKey. It flushes the keyboard *)
- (* buffer first, in case you had accidentally pressed a key before *)
- (* it was called. Then it looks for a key, and waits until one is *)
- (* pressed. If the key code it detects is a byte with the value of *)
- (* zero, that means the key is a function key or other special *)
- (* key, and there is another byte waiting, so GetKey gets the second *)
- (* byte and returns that to the calling routine. In this case the *)
- (* value returned is the scan key of the key pressed. If there is *)
- (* no first byte of zero GetKey returns whatever byte is in the *)
- (* buffer, which is the ordinal of the character representing the *)
- (* pressed. As an ordinal function such as a character function *)
- (* actually returns it value in the AL register to the calling *)
- (* routine, the ReadKey := C is not really necessary, but is included *)
- (* for clarity. Hope this is of some value to you. It adds less than *)
- (* 50 bytes to your program if called once. *)
- (* *)
- (* Rick Housh CIS PIN 72466,212 *)
- (************************************************************************)
-
- Program GetKeyDemo;
- var ch : char;
-
- Function GetKey : char;
- var C : char;
- Begin
- Inline(
- {; Function GetKey : Char}
- {; Clears the keyboard buffer then waits until}
- {; a key is struck. If the key is a special, e.g.}
- {; function key, goes back and reads the next}
- {; byte in the keyboard buffer. Thus does}
- {; nothing special with function keys.}
- $B4/$0C { MOV AH,$0C ;Set up to clear buffer}
- /$B0/$08 { MOV AL,8 ;then to get a char}
- /$CD/$21 {SPCL: INT $21 ;Call DOS}
- /$3C/$00 { CMP AL,0 ;If it's a 0 byte then}
- /$75/$04 { JNZ CHRDY ;is spec. key, get second byte}
- /$B4/$08 { MOV AH,8 ;else set up for another}
- /$EB/$F6 { JMP SHORT SPCL ;and get it}
- /$88/$46/<C {CHRDY: MOV <C[BP],AL ;else put into function return}
- );
- GetKey := C;
- end; {Inline function GetKey}
-
- Begin {Program GetKeyDemo}
- WriteLn;
- Write('Press a key: ');
- ch := GetKey;
- WriteLn(ch);
- end.