Discussion:
How to "Press any key to continue..."
Richard Le Mesurier (Investec Employee Benefits)
2002-05-31 08:53:34 UTC
Permalink
My setup app is a console application. If it isn't run from the correct
folder, it must alert the user and abort.

I writeln a couple of lines, followed by the usual DOS message of 'Press any
key to continue...'

How do I detect a keypress in a console app? We used to use Inkey or Readkey
in Turbo Pascal I think - what is the Delphi equivalent?

Or is there a "better" way of doing this?

Thanx
Stephen Wood
2002-05-31 08:51:48 UTC
Permalink
Delphi uses ReadLn....

-----Original Message-----
From: Richard Le Mesurier (Investec Employee Benefits)
[mailto:***@investec.co.za]
Sent: 31 May 2002 10:54
To: 'delphi-***@yahoogroups.com'
Subject: [Delphi] How to "Press any key to continue..."


My setup app is a console application. If it isn't run from the correct
folder, it must alert the user and abort.

I writeln a couple of lines, followed by the usual DOS message of 'Press any
key to continue...'

How do I detect a keypress in a console app? We used to use Inkey or Readkey
in Turbo Pascal I think - what is the Delphi equivalent?

Or is there a "better" way of doing this?

Thanx



The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Richard Le Mesurier (Investec Employee Benefits)
2002-05-31 09:00:36 UTC
Permalink
Doesn't that read in a line of text typed up to the point that Enter is
pressed?

I am looking for it to wait until ANY key is pressed (hoping that the user
won't start looking for a key with "any" written on it...:)
Stephen Wood
2002-05-31 09:26:08 UTC
Permalink
Then just use Read, I think....

-----Original Message-----
From: Richard Le Mesurier (Investec Employee Benefits)
[mailto:***@investec.co.za]
Sent: 31 May 2002 11:01
To: 'delphi-***@yahoogroups.com'
Subject: RE: [Delphi] How to "Press any key to continue..."


Doesn't that read in a line of text typed up to the point that Enter is
pressed?

I am looking for it to wait until ANY key is pressed (hoping that the user
won't start looking for a key with "any" written on it...:)



The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Richard Le Mesurier (Investec Employee Benefits)
2002-05-31 09:34:50 UTC
Permalink
Nope, it actually does the same thing, except Readln moves the cursor down a
line...

I remember now that I am looking for the Delphi or API version of

repeat
until KeyPressed;

Anyone?
mardirossianfr
2002-05-31 09:47:24 UTC
Permalink
...
...
implementation

const
FKeys = [#59..#68];
SKeys = [vk_shift,vk_control,vk_menu,vk_capital];
Ctrl_Pressed = Left_Ctrl_Pressed OR Right_Ctrl_Pressed;
Alt_Pressed = Left_Alt_Pressed OR Right_Alt_Pressed;
var
InRec:TInputRecord;
LastKey:Char=#32;
LastScan:Char=#32;
CurMode,NormAttr:Word;
hStdIn:THandle = 0;
hStdOut:THandle = 0;
hStdErr:THandle = 0;
hConsole:Thandle = 0;
BI : TConsoleScreenBufferInfo;
AltKey:AnsiString = '-0123456789=abcdefghijklmnopqrstuvwxyz';
CvtKey:AnsiString = #130#129#120#121#122#123#124#125#126#127#128+
#131#30#48#46#32#18#33#34#35#23#36#37#38+
#50#49#24#25#16#19#31#20#22#47#17#45#21#44;
....
....
function KeyPressed : Boolean;
var
I : DWord;
begin
GetNumberofConsoleInputEvents(hStdIn,I);
Result:=I>0;
end;


function ReadKey : Char;
var
I : DWord;
OK : Boolean;
begin
if LastKey=#0 then
Result:=LastScan
else begin
{$ifdef VER120}
with InRec.Event.KeyEvent do begin
{$else}
with InRec.KeyEvent do begin
{$endif}
repeat
repeat
Ok:=ReadConsoleInput(hStdIn,InRec,1,I);
until OK and (InRec.EventType=KEY_EVENT) and
(bKeyDown=False);
LastScan:=Char(wVirtualScanCode);
until not (wVirtualKeyCode in SKeys);
Result:=AsciiChar;
if dwControlKeyState<>0 then begin
if Result=#0 then begin
if (dwControlKeyState and Shift_Pressed)<>0 then begin
if LastScan in FKeys then LastScan:=Char(Ord(LastScan)
+25);
end else if (dwControlKeyState and Ctrl_Pressed)<>0 then
begin
if LastScan in FKeys then
LastScan:=Char(Ord(LastScan)+35)
else case LastScan of
#55:LastScan:=#114;
#73:LastScan:=#132;
#75:LastScan:=#115;
#77:LastScan:=#116;
#79:LastScan:=#117;
#81:LastScan:=#118;
end;
end else if (dwControlKeyState and Alt_Pressed)<>0 then
begin
if LastScan in FKeys then LastScan:=Char(Ord(LastScan)
+45);
end;
end else begin
if (dwControlKeyState and Ctrl_Pressed)<>0 then begin
if Result in ['a'..'z'] then Result:=Char(Ord(Result)-
96)
end else if (dwControlKeyState and Alt_Pressed)<>0 then
begin
I:=Pos(Result,AltKey);
if I>0 then begin
LastScan:=CvtKey[I];
Result:=#0;
end;
end else if (dwControlKeyState and Shift_Pressed)<>0 then
begin
if Result=#9 then begin
Result:=#0;
LastScan:=#15;
end else CharUpper(PChar(Result));
end;
end;
end;
end;
end;
LastKey:=Result;
end;

Try a google searcg , if you can't find that i can send you the file,
or upload it.

patrick
Post by Richard Le Mesurier (Investec Employee Benefits)
Nope, it actually does the same thing, except Readln moves the
cursor down a
Post by Richard Le Mesurier (Investec Employee Benefits)
line...
I remember now that I am looking for the Delphi or API version of
repeat
until KeyPressed;
Anyone?
Richard Le Mesurier (Investec Employee Benefits)
2002-05-31 09:55:51 UTC
Permalink
Thanks, I found their website and am about to download the file.

So glad I am not expected to understand that code you posted - my first
reaction was to panic when I read it...
Martin Wynne
2002-05-31 11:06:50 UTC
Permalink
Post by Richard Le Mesurier (Investec Employee Benefits)
So glad I am not expected to understand that
code you posted - my first reaction was to panic
when I read it...
I use this:

procedure wait_any_key;

var
n:byte;

begin
repeat
for n:=0 to 255 do begin
GetAsyncKeyState(n); // clear first
if GetAsyncKeyState(n)<>0 then EXIT; // key n is down.
end;//for
until 0<>0;
end;//proc

Martin.
Stephen Wood
2002-05-31 10:38:45 UTC
Permalink
Found it for you...after tinkering in the API's....

var
AHnd : THandle;
AChar : char;
begin
AHnd := GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(AHnd,0);
Writeln('Press any key');
Read (AChar);
end;

You first gotta get the console handle, the set the console mode to remove
the flag that says wait for a CRLF....

Give it a go....

-----Original Message-----
From: Richard Le Mesurier (Investec Employee Benefits)
[mailto:***@investec.co.za]
Sent: 31 May 2002 11:35
To: 'delphi-***@yahoogroups.com'
Subject: RE: [Delphi] How to "Press any key to continue..."


Nope, it actually does the same thing, except Readln moves the cursor down a
line...

I remember now that I am looking for the Delphi or API version of

repeat
until KeyPressed;

Anyone?



The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Stephen Wood
2002-05-31 11:11:20 UTC
Permalink
OK, another way...Is GetAsyncKeyState safe in a console app?

Basically what I mean is that if you run it in command prompt mode will it
still work?

-----Original Message-----
From: Martin Wynne [mailto:***@ision.co.uk]
Sent: 31 May 2002 01:07
To: delphi-***@yahoogroups.com
Subject: Re: [Delphi] How to "Press any key to continue..."
Post by Richard Le Mesurier (Investec Employee Benefits)
So glad I am not expected to understand that
code you posted - my first reaction was to panic
when I read it...
I use this:

procedure wait_any_key;

var
n:byte;

begin
repeat
for n:=0 to 255 do begin
GetAsyncKeyState(n); // clear first
if GetAsyncKeyState(n)<>0 then EXIT; // key n is down.
end;//for
until 0<>0;
end;//proc

Martin.




The Delphi Top 100 Web Sites!
http://www.sandbrooksoftware.com/TS/TS2/delphi.shtml
---------------------------------------------------------------
Unsubscribe:delphi-programming-***@yahoogroups.com
List owner:delphi-programming-***@yahoogroups.com
---------------------------------------------------------------

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Loading...