[comp.lang.pascal] CTRL-Characters

storm@cs.mcgill.ca (Marc WANDSCHNEIDER) (03/28/91)

How do I make a PASCAL program recognize CTRL characters (or ALT characters 
for that matter).  Ie I want the following:

     IF CH = CTRL-G then 
          BEGIN 
           blahblahb
	  END;


Any ideas...?

./*-
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
storm@cs.mcgill.ca         McGill University           It's 11pm, do YOU
Marc Wandschneider         Montreal, CANADA            know what time it is?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Jeff Boyd <BOYDJ@QUCDN.QueensU.CA> (03/28/91)

Here's a quick program you can play around with. It was written under
TP5.0 for a Zenith AT clone. After invoking it, simply press keys or
combinations of keys and Alt, Ctrl, or Shift. This was a 5 minute hack,
so comments on my coding style will be ignored :-). Execution stops
after Alt-F10 (normal) or Ctrl-Break (abnormal).

{======================================================================}
program KeyTest (input,output) ;

uses  Crt ;

var   KeyStroke :  char ;
      ExtendKey :  boolean ;

{======================================================================}
begin  {Main_Program}

repeat
   KeyStroke := ReadKey ;
   if (KeyStroke<>#0) then begin
      ExtendKey := FALSE ;
      writeln ('ReadKey: ',KeyStroke,' ',ord(KeyStroke)) ;
      end
   else begin
      ExtendKey := TRUE ;
      KeyStroke := ReadKey ;
      writeln ('ReadKey: ',KeyStroke,' ','0;',ord(KeyStroke)) ;
   end ;
until ((ExtendKey) and (KeyStroke=#113)) ;  {* that's ALT-F10 to exit *}

end .  {Main_Program}
{======================================================================}

wjt@oz.plymouth.edu (Bill Taffe) (03/28/91)

In article <1991Mar27.161213.2100@cs.mcgill.ca> storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
>
>
>How do I make a PASCAL program recognize CTRL characters (or ALT characters 
>for that matter).  Ie I want the following:
>
>     IF CH = CTRL-G then 
>          BEGIN 
>           blahblahb
>	  END;


The TP5.5 manual (which I happen to have handy) has a section
describing how to do this - or some of these anyhow.

There are "extended key codes"  (see p424  ... table c.2) which give
special codes for many ALT and CTRL keys.  See also the readkey
function on p337.  When a special key is pressed, readkey first
returns a null character #0 and then returns the extended scan code.
There is an example on p 338.

Bill Taffe
Plymouth State College (NH)
wjt@oz.plymouth.edu

cd5340@mars.njit.edu (David Charlap) (03/28/91)

In article <1991Mar27.161213.2100@cs.mcgill.ca> storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
>
>
>How do I make a PASCAL program recognize CTRL characters (or ALT characters 
>for that matter).  Ie I want the following:
>
>     IF CH = CTRL-G then 
>          BEGIN 
>           blahblahb
>	  END;

Simple. Ctrl-Codes are ascii values 1-31 representing ^A-^_.  Note
that certain special keys "map" onto other Control codes.  For
example: ^M=<Return>=Ascii 13, ^I=<Tab>=Ascii 9, ^[=<Esc>=Ascii 27,
etc.  So, your example would be:

Var
	Ch : Char;
...
	If Ch = #7 then.....

To scan for an Alt-Key, you need to use Turbo Pascal's extended
scan codes.  If the character read is ASCII-0  (#0), then the
key pressed is a special "extended" key.  The next "Character" read
will be the extended key code.  Consult the chart in the back of
your manual for the extended codes.  It's on page 424 in the Turbo
Pascal 5.0 reference guide.
--
David Charlap                   "Invention is the mother of necessity"
cd5340@mars.njit.edu            "Necessity is a mother"
Operators are standing by	"mother!" - Daffy Duck

dave@tygra.UUCP (David Conrad) (03/28/91)

In article <1991Mar27.161213.2100@cs.mcgill.ca> storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
>
>
>How do I make a PASCAL program recognize CTRL characters (or ALT characters 
>for that matter).  Ie I want the following:
>
>     IF CH = CTRL-G then 
>          BEGIN 
>           blahblahb
>	  END;
>
>
>Any ideas...?

Yes, I've got an idea: WHY DON'T YOU READ THE F*CKING MANUAL (RTFM)?

  If ch = ^G then          (* or ch = #7 *)
    begin
      Enter (Pnews);
      Ask (Question.Dumb);
    end;

Dave "Flame" Conrad
dave%tygra@sharkey.cc.umich.edu
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave%tygra@sharkey.cc.umich.edu

einstein@cs.mcgill.ca (Michael CHOWET) (03/30/91)

In article <1991Mar27.161213.2100@cs.mcgill.ca> storm@cs.mcgill.ca 
(Marc WANDSCHNEIDER) writes:

>How do I make a PASCAL program recognize CTRL characters (or ALT characters 
>for that matter).  Ie I want the following:

>     IF CH = CTRL-G then 
>          BEGIN 
>           blahblahb
>	  END;

  Actually, there are two ways to get at this. If you feel like dragging out
the list of ASCII codes, you can type:

  If CH = #07 Then DoSomething ; 

  Basically, the # symbol followed by the ASCII code. Simple enough, right?

  But if you want the actual character in there, hit the ^P. The following
character (your CTRL-char) will be accepted as a literal, ie it will be
inserted into the text without trying to process it as an editor control key.

  Hope that helps.

dave@tygra.UUCP (David Conrad) (04/01/91)

In article <1991Mar30.081820.8305@cs.mcgill.ca> einstein@cs.mcgill.ca (Michael CHOWET) writes:
>In article <1991Mar27.161213.2100@cs.mcgill.ca> storm@cs.mcgill.ca 
>(Marc WANDSCHNEIDER) writes:
>
>>How do I make a PASCAL program recognize CTRL characters (or ALT characters 
>>for that matter).  Ie I want the following:
>
>>     IF CH = CTRL-G then 
>>          BEGIN 
>>           blahblahb
>>	  END;
>
>  Actually, there are two ways to get at this. If you feel like dragging out
>the list of ASCII codes, you can type:
>
>  If CH = #07 Then DoSomething ; 
>
>  Basically, the # symbol followed by the ASCII code. Simple enough, right?
>
>  But if you want the actual character in there, hit the ^P. The following
>character (your CTRL-char) will be accepted as a literal, ie it will be
>inserted into the text without trying to process it as an editor control key.
>
>  Hope that helps.

This is rather ambiguous.  "If ch = 'G' then" works where "G" is a control
character entered by pressing CTRL-P CTRL-G.  But "If ch = G then" where
"G" is once again a control character will not even compile.  The former,
which does work, will probably not print, though.  One that works and
can be sent to the console or printer is "If ch = ^G then" or the above-
mentioned "If ch = #7 then".

Dave Conrad
dave%tygra@sharkey.cc.umich.edu
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave%tygra@sharkey.cc.umich.edu

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (04/02/91)

>/ hpcupt3:comp.lang.pascal / einstein@cs.mcgill.ca (Michael CHOWET) / 12:18 am  Mar 30, 1991 /
>In article <1991Mar27.161213.2100@cs.mcgill.ca> storm@cs.mcgill.ca 
>(Marc WANDSCHNEIDER) writes:
>
>>How do I make a PASCAL program recognize CTRL characters (or ALT characters 
>>for that matter).  Ie I want the following:
>
>>     IF CH = CTRL-G then 
>>          BEGIN 
>>           blahblahb
>>	  END;
>
>  Actually, there are two ways to get at this. If you feel like dragging out
>the list of ASCII codes, you can type:
>
>  If CH = #07 Then DoSomething ; 
>
>  Basically, the # symbol followed by the ASCII code. Simple enough, right?
>
>  But if you want the actual character in there, hit the ^P. The following
>character (your CTRL-char) will be accepted as a literal, ie it will be
>inserted into the text without trying to process it as an editor control key.

IMHO this is asking for trouble and curses from fellow programmers.  What I do
is:

Unit ASCII;

Interface

Const
   ControlA    = #01;
   ControlB    = #02;

   ...

   ControlG    = #07;

Implementation

End. { ASCII }

Program Foo;

Uses ASCII;

Var
   Character : Char;

Begin { Foo }
   Character := ReadKey;

   If Character = ControlG then
      { Do want you want }
   else
      { Do something else }

End. { Foo }

If you define all ASCII characters in the Unit ASCII then you can easily
refer to them *symbolically* by simply including the ASCII unit in your uses
statement.

Note that Control characters do NOT have a scan code.  Function keys, and all
there derivitives like ControlF1, AltF2, etc, do.  What I have done is wrote a
more generic GETKEY function that handles these things and returns things like
ControlF1 or AltF2, which I have defined in the ASCII unit as something like:

Const
   ControlF1 = #170;
   ControlF2 = #171;
   ...

   AltF1     = #180;
   AltF2     = #181;

I'm pretty sure that the characters #170 on up aren't used (although they are
probably 8-bit characters used for other language sets but I'm not concerned
with them).