I'm writing a console application and need some user input, like this:
var Answer: Char;
Write('File already exists. Overwrite? (Y/N):');
Read(Answer);
And it works, but it waits for Enter to be pressed. I remember in good old
pascal when we needed a single char from the user we used to do it like
this:
while not KeyPressed do;
But there's no KeyPressed in Delphi (at least I couldn't find it), and it's
a crime to use loops like that in Windows.
So how do I do it in Windows console application?
Cheers,
Alex
> And it works, but it waits for Enter to be pressed. I remember in good
> old pascal when we needed a single char from the user we used to do it
> like this:
There are various free CRT or replacement units available for Delphi. I
suggest you try one of those. They usually come with routines like
KeyPressed and ReadKey, using the Console range of API functions.
If you are interested, you could of course write your own, using these
API functions as well. It is not very hard.
--
Rudy Velthuis (TeamB)
"Ask her to wait a moment - I am almost done."
-- Carl Friedrich Gauss (1777-1855), while working, when informed that
his wife is dying
> And it works, but it waits for Enter to be pressed. I remember in good
> old pascal when we needed a single char from the user we used to do it
> like this:
>
> while not KeyPressed do;
uses Windows;
function KeyPressed: Boolean;
var
NumEvents: Integer;
begin
GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_HANDLE),
DWORD(NumEvents));
Result := NumEvents > 0;
end;
> So how do I do it in Windows console application?
I've put together a replacement Crt unit for Delphi (works in Delphi
2-7, and includes documentation for all the functions) and it includes
ReadKey and KeyPressed. You can get it from CodeCentral at the
following URL (mind the wrapping)--
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=19810
It's listed under ID 19810, 'Crt replacement for Delphi'. It also
includes a number of useful functions for console apps such as routines
to save and restore the screen (or the entire Crt/console state (cursor
location, current text attributes, etc)), as well as routines to test
the status of the shift/alt/ctrl keys (which are relative to the last
ReadKey performed, a mixed blessing I suppose, it allows you to test
for key combinations such as Ctrl+Alt+C, but doesn't allow you to test
for actual Shift presses). Hope this helps.
Will
> function KeyPressed: Boolean;
> var
> NumEvents: Integer;
> begin
> GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_HANDLE),
> DWORD(NumEvents));
> Result := NumEvents > 0;
> end;
Unfortunately, for me anyways on Windows 2000, input events seems to
include losing/getting the focus, and other events that wouldn't
qualify as a key press (pressing and releasing Shift, for example).
But still, it'll work in a pinch I suppose. =)
Will
Alex
"Alexander Bach" <al...@softhome.net> wrote in message
news:3eac...@newsgroups.borland.com...
> Unfortunately, for me anyways on Windows 2000, input events seems to
> include losing/getting the focus, and other events that wouldn't
> qualify as a key press (pressing and releasing Shift, for example).
> But still, it'll work in a pinch I suppose. =)
I forgot to say that this was untested, and out of memory. I guess a
further test for the kind of events is in order to avoid the problems you
mention.