Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uwm.edu!bionet!agate!ucbvax!bloom-beacon!mit-eddie!uw-beaver!zephyr.ens.tek.com!vice!bobb From: bobb@vice.ICO.TEK.COM (Bob Beauchaine) Newsgroups: comp.lang.pascal Subject: Re: Extracting from a unit (or CRT vs. ANSI) Message-ID: <7228@vice.ICO.TEK.COM> Date: 5 Apr 91 01:18:34 GMT References: <26447@adm.brl.mil> Reply-To: bobb@vice.ICO.TEK.COM (Bob Beauchaine) Organization: Tektronix, Inc., Beaverton, OR. Lines: 72 In article <26447@adm.brl.mil> 209507097@ucis.vill.edu (MCREE, JAMES F) writes: > > I need to use the "KeyPressed" and "ReadKey" functions in CRT in Turbo >5.0, but can't use the CRT unit because it will cause the program to ignore the >meaning of my ANSI codes. There are three general ways that I can think of for >getting this to work. > The easiest way to do this is to rewrite the KeyPressed and Readkey functions. It's really quite simple in Turbo 5.0: Rewriting the ReadKey function is actually a benefit for the Turbo programmer, since (IMHO) it provides more functionality. function keyread : word; { This function uses BIOS call $16 to read a key from the keyboard. if no key is waiting in the buffer, it waits for a keystroke. The keyboard scan code is returned in Ah, the ascii value in Al. The return value is the combination of both, or the Ax register } var regs : registers; begin regs.ah := 0; intr($16,regs); keyread := regs.ax; end; All of the "normal" keys will return a number equivalent to their ascii value in the low byte of the function return. If the low byte of the result is not 0, you have a valid ascii value in the low byte. If the low byte is 0, you have to decode the high byte (scan code) to determine the key pressed. This makes handling Ctrl and Alt key combinations particularly easy, since all valid keyboard inputs produce a unique scan code. As for the keypressed function: function pressedkey : boolean; var regs : registers; begin regs.ah := 1; intr($16,regs); pressedkey := (regs.flags and Fzero) <> 0; end; Interrupt $16, function 1 checks to see if a key press is waiting in the keyboard buffer. If a keystroke is pending, the zero flag gets set, which you can check with the Fzero mask. Disclaimers and Hints: I have not tested the pressedkey function. It should work, but there is the possibility that calling the pressedkey function will obliterate the waiting keystroke. My BIOS reference states that if a key is waiting and function 1 is called, the scan code and ascii value are returned in the AX register exactly like the keyread function. It is not clear to me if the keystroke is *removed* from the buffer or not. You'll have to find out for yourself :). If you want to compile these functions, you will need to include a "Uses Dos;" statement in your program, since the appropriate types and constants are in the Dos unit. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Bob Beauchaine bobb@vice.ICO.TEK.COM C: The language that combines the power of assembly language with the flexibility of assembly language.