[comp.lang.pascal] Extracting from a unit

209507097@ucis.vill.edu (MCREE, JAMES F) (04/04/91)

	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.

	1. Extract these two functions directly out of the CRT unit and compile 
           them separately or include the code directly in my program.
	2. Write new code or reverse-engineered code to perform these 
           functions.
	3. Tell CRT to ignore ANSI escpape sequences.

However, there is a problem.  At the moment, I don't know how to do any of 
these.  Any ideas out there?  Feel free to add on 4,5,... ideas.

				Thanks for any help you can provide,

						Jim.

TOMJ@csdserver3.csd.scarolina.edu (Thomas E. Jenkins, Jr.) (04/04/91)

Hi,

  Try this:

  4.  Use Intr to call BIOS routines to perform your own ReadKey &
      KeyPressed.


 USES DOS ;


 FUNCTION    KeyPressed                          : BOOLEAN ;

  VAR
       regs                                      : Registers ;

   BEGIN

     FillChar ( regs , SizeOf ( regs ) , #0 ) ;

     regs.ah := $01 ;           {  func 01h will wait on key to be pressed  }
                                {  if buffer is empty.  func 11h for        }
                                {  extended keyboards                       }
     Intr ( $016 , regs ) ;

     KeyPressed := ( regs.flags AND FZero ) = 0 ;

     END ;  {  KeyPressed  }



 FUNCTION    ReadKey                             : CHAR ;

  VAR
       regs                                      : Registers ;

   BEGIN

     FillChar ( regs , SizeOf ( regs ) , #0 ) ;

     regs.ah := $00 ;

     Intr ( $16 , regs ) ;

     ReadKey := Chr ( regs.ah ) ;   {  note al has scan code if needed  }

     END ;  {  ReadKey  }


Hope this helps,

tom
+--------------------------------------------------------------------------+
|  Thomas E. Jenkins, Jr.   Programmer, University of South Carolina CSD   |
+--------------------------------------------------------------------------+
| BITNET         :  C0361@UNIVSCVM.BITNET  |  CSDNET  :  tomj/csdserver3   |
| INTERNET       :  TOMJ@csdserver3.csd.scarolina.EDU          {PREFERRED} |
|                :  C0361@univscvm.csd.scarolina.EDU  |  129.252.43.30     |
| FROM Compuserv :  INTERNET:TOMJ@csdserver3.csd.scarolina.EDU {PREFERRED} |
|                :  INTERNET:C0361@univscvm.csd.scarolina.EDU              |
+--------------------------------------------------------------------------+

tsmith@plains.NoDak.edu (Timothy Lyle Smith) (04/05/91)

If you want to implement keypressed and/or readkey without the CRT unit
look at the code for keypressed or readkey in the file CRTG.ZIP on
garbo.uwasa.fi.  I know that it works because I wrote it.  It will also
let you detect Ctrl-Break.

It does not use interrupts but looks at the keyboard buffer and indecies
that keep track of where the key presses are and will be.


-- 
Tim Smith  
Minard 300                      UUCP:        ...!uunet!plains!tsmith
North Dakota State University,  BITNET:      tsmith@plains.bitnet
Fargo, ND  58105                INTERNET:    tsmith@plains.NoDak.edu

ts@uwasa.fi (Timo Salmi) (04/05/91)

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.
:
:
>However, there is a problem.  At the moment, I don't know how to do any of 
>these.  Any ideas out there?  Feel free to add on 4,5,... ideas.

This problem (and many other TP routine problems that keep popping
up in comp.lang.pascal) has already been solved quite awhile ago for
public consumption.  Download the /pc/ts/tspa23##.arc Turbo Pascal
units collection (##=40,50,55,60) from our archives for these
routines, and many others. 

...................................................................
Prof. Timo Salmi        
Moderating at garbo.uwasa.fi anonymous ftp archives 128.214.12.37
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

bobb@vice.ICO.TEK.COM (Bob Beauchaine) (04/05/91)

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.

amead@s.psych.uiuc.edu (alan mead) (04/06/91)

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 
>...

I have an ANSI.SYS driven CRT replacement unit that I got from (I
think) SIMTEL that sounds like what you want--just what ANSI codes does
CRT cause your code to ignore?!

I believe the unit is called ANCRT and I can upload it if it is no
longer available (or if I'm wrong and I didn't get it from SIMTEL).

If you don't want to use the whole unit, the source is included, so you
can see what ReadKey does.  In fact, there is a better function GetKey
(or something) that may vbe helpfull (like perhaps it is ReadKey and
not the CRT unit that is ignoring ANSI codes).

-alan mead : amead@s.psych.uiuc.edu

phys169@csc.canterbury.ac.nz (04/10/91)

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.
> 
> 	1. Extract these two functions directly out of the CRT unit and compile 
>            them separately or include the code directly in my program.
Not worth trying, especailly as there is important other stuff in the unit you
might miss, e.g. during startup.

> 	2. Write new code or reverse-engineered code to perform these 
>            functions.
Pretty simple, many others have supplied examples. They're short routines, and
worth using in-line assembler (ask me for a copy if you want my versions of
these routines).

> 	3. Tell CRT to ignore ANSI escape sequences.
> 
Probably the best idea for what you're after, you can use the CRT unit and
still write to the standard output, e.g. assign (output,''); rewrite(output).

There are some related questions worth talking about, though...

1. Why do you want to use ANSI escape sequences? If the answer is because you
might want to use the CTTY command to run the program from a terminal on a coms
port, then the above suggestions about ReadKey and KeyPressed (using BIOS
interrupt $16) are invalid, and you should test for the standard input being
ready. It's probably unlikely that's what you're after, but just in case it is,
there are DOS calls that do a pretty good job. If you want to redefine function
keys, consider assign(input,''); reset(input) as well.

2. Some poeople want to use the features of the CRT unit but avoid the risk of
crashing during startup on incompatible hardware (e.g. the Data General DG10 -
but not DG1 - falls over in a major way during the automatic startup code that
is executed when you include the CRT unit). I have a unit that I could make
public, that duplicates most of the CRT unit but runs okay on some "odd"
computers like the DG10 and Sanyo MBC550's. Let me know if anyone wants it...

Hope this helps,
Mark Aitchison, Physics, University of Canterbury, New Zealand.