Discussion:
Question: how to read any one character from keyboard
(too old to reply)
tbone
2007-05-17 00:50:02 UTC
Permalink
hmm... all the Delphi groups but this one are essentially silent.
Doesn't anyone use Delphi anymore?

I've been given a Delphi 4 project, and I'm new to Delphi (I used to
know Pascal). This program has many quirks, one of which is prompting
the user at one point to "press any key" (it's a console app and this
is after an error message at startup).

The Read procedure is used to read one character from the keyboard.
Trouble is, you can type any character you want, but the only thing
that works is the Enter key or control-C.

The help says that if you use Read with a Char var, it reads one
character, whereas with a string var, it reads up to a newline. My
experience here seems to indicate that it doesn't matter - it still
waits for a newline.

To wit: ----------------

procedure ShowHelp;
var aKey: char;
begin
writeln;
writeln;
... help messages displayed with writelns ...
writeln('Press any key...');
read(aKey); { doesn't return until you hit Enter}
{keys are echoed in console window }
end;

Any pointers? I know Delphi 4 is ancient but I don't have
authorization to upgrade to anything newer (yet).

Thanks
tbone
Jamie
2007-05-17 01:55:02 UTC
Permalink
Post by tbone
hmm... all the Delphi groups but this one are essentially silent.
Doesn't anyone use Delphi anymore?
I've been given a Delphi 4 project, and I'm new to Delphi (I used to
know Pascal). This program has many quirks, one of which is prompting
the user at one point to "press any key" (it's a console app and this
is after an error message at startup).
The Read procedure is used to read one character from the keyboard.
Trouble is, you can type any character you want, but the only thing
that works is the Enter key or control-C.
The help says that if you use Read with a Char var, it reads one
character, whereas with a string var, it reads up to a newline. My
experience here seems to indicate that it doesn't matter - it still
waits for a newline.
To wit: ----------------
procedure ShowHelp;
var aKey: char;
begin
writeln;
writeln;
... help messages displayed with writelns ...
writeln('Press any key...');
read(aKey); { doesn't return until you hit Enter}
{keys are echoed in console window }
end;
Any pointers? I know Delphi 4 is ancient but I don't have
authorization to upgrade to anything newer (yet).
Thanks
tbone
I don't know what to tell you, I just tested simple console app with
D3..
-- It works as expected ---
program Project1;
uses windows;
{$APPTYPE CONSOLE}
Var
A:Char;
begin
Repeat
Read(A); //get key if any, otherwise, returns null char.
Write(A);// writes to the screen.
Until A = #13;
end.

read does not wait for characters., it returns empty if
no chars are in the keyboard buffer.
Readln how ever, will wait.
if you're waiting for a response from the user try this..

Repeat Read(A) Until A<>#0;

Also, Read does not echo.., is it possible Read is just
following through as expect to the next line of code or
some code that is using Write(AKey) ?

P.S.
You can create a console window with in a GUI app at the
same time.., Using the AllocConsole.
This makes it possible to have both GUI and console window
running at the same time.
Just remember to use FreeConsole when done.
--
"I'm never wrong, once i thought i was, but was mistaken"
Real Programmers Do things like this.
http://webpages.charter.net/jamie_5
tbone
2007-05-21 19:30:33 UTC
Permalink
On Wed, 16 May 2007 21:55:02 -0400, Jamie
Post by Jamie
I don't know what to tell you, I just tested simple console app with
D3..
-- It works as expected ---
program Project1;
uses windows;
{$APPTYPE CONSOLE}
Var
A:Char;
begin
Repeat
Read(A); //get key if any, otherwise, returns null char.
Write(A);// writes to the screen.
Until A = #13;
end.
read does not wait for characters., it returns empty if
no chars are in the keyboard buffer.
Readln how ever, will wait.
if you're waiting for a response from the user try this..
Repeat Read(A) Until A<>#0;
Also, Read does not echo.., is it possible Read is just
following through as expect to the next line of code or
some code that is using Write(AKey) ?
hmm.. maybe v4 is different? In stepping over the "read(aKey)" line
with the debugger, the next statement is not reached until I hit
Enter, and characters typed are echoed in the console window. After I
hit enter, the first key I typed is in variable aKey.

Maybe there are some default STDIN and STDOUT buffering behaviors
changed between D3 and D4?

Others have suggested a few things to try, so I have something to go
on.

thanks
tbone
Heinrich Wolf
2007-05-23 05:43:47 UTC
Permalink
"Jamie" <***@charter.net> schrieb im
Newsbeitrag news:WrO2i.445$***@newsfe02.lga...
...
Post by Jamie
I don't know what to tell you, I just tested simple console app with
D3..
-- It works as expected ---
program Project1;
uses windows;
{$APPTYPE CONSOLE}
Var
A:Char;
begin
Repeat
Read(A); //get key if any, otherwise, returns null char.
Write(A);// writes to the screen.
Until A = #13;
end.
...
If you remove the statement "Write(A);",
the behaviour in the IDE seems to be the same.
I'm afraid, the echo is not done by Write, but by Read
and the Until is not reached on any key
before you finally type the enter key.

You don't see the difference in the IDE,
because the console window closes on ending the application.

After you compiled the project,
open a console, go to the project folder
and call the application.
On ending it, that console doesn't close.
You will see, that all writes are done
not before you typed the final enter key.

Regards
Heiner
Rob Kennedy
2007-05-17 01:48:05 UTC
Permalink
Post by tbone
hmm... all the Delphi groups but this one are essentially silent.
Doesn't anyone use Delphi anymore?
Of the 10 newsgroups with "Delphi" in their names that I read, I
estimate eight of them get messages every day. You're obviously not
looking on the CodeGear servers.
Post by tbone
I've been given a Delphi 4 project, and I'm new to Delphi (I used to
know Pascal). This program has many quirks, one of which is prompting
the user at one point to "press any key" (it's a console app and this
is after an error message at startup).
The Read procedure is used to read one character from the keyboard.
Trouble is, you can type any character you want, but the only thing
that works is the Enter key or control-C.
The help says that if you use Read with a Char var, it reads one
character, whereas with a string var, it reads up to a newline. My
experience here seems to indicate that it doesn't matter - it still
waits for a newline.
It *reads* one character, but it doesn't know about that one character
until Enter has been pressed -- the OS doesn't return to the application
until then. Any additional characters are left on the input buffer, so
you can read them later.
Post by tbone
To wit: ----------------
procedure ShowHelp;
var aKey: char;
begin
writeln;
writeln;
... help messages displayed with writelns ...
writeln('Press any key...');
read(aKey); { doesn't return until you hit Enter}
{keys are echoed in console window }
You might remember the KeyPressed or ReadKey functions. Try the versions
from Rudy Velthuis's Console.pas unit.

http://rvelthuis.de/programs/console.html
--
Rob
tbone
2007-05-21 19:09:07 UTC
Permalink
Post by Rob Kennedy
Of the 10 newsgroups with "Delphi" in their names that I read, I
estimate eight of them get messages every day. You're obviously not
looking on the CodeGear servers.
Since you didn't provide a URL, I located the CodeGear website. After
poking around a bit there, it seems that you're ultimately referring
to the borland.public.delphi... groups, which are available on my news
server.

80% of these have nothing but a handful of spam. The one group with
more than a few hundred messages since 1/1/07 is called
"...non-technical" which didn't sound like it was of any interest.

So while it's indeed obvious I'm not looking on the CodeGear servers,
it's certainly not obvious exactly where that enticing newsgroup
content is located.
Post by Rob Kennedy
You might remember the KeyPressed or ReadKey functions. Try the versions
from Rudy Velthuis's Console.pas unit.
http://rvelthuis.de/programs/console.html
As I mentioned, I'm not at all familiar with any version of Delph.
This URL looks like it will be very helpful.

Thanks
tbone
Rob Kennedy
2007-05-21 19:36:29 UTC
Permalink
Post by tbone
Post by Rob Kennedy
Of the 10 newsgroups with "Delphi" in their names that I read, I
estimate eight of them get messages every day. You're obviously not
looking on the CodeGear servers.
Since you didn't provide a URL, I located the CodeGear website.
To provide a URL, I would have had to use a search engine. You're just
as capable of asking Google for "codegear newsgroups" as I am.
Post by tbone
After
poking around a bit there, it seems that you're ultimately referring
to the borland.public.delphi... groups, which are available on my news
server.
Your ISP is doing you a disservice by providing them on its news server.
CodeGear's news servers do not accept submissions from outside sources;
you must post directly to CodeGear's own servers. Messages posted to
your ISP's server are not propagated for CodeGear's server, and since
all users read from CodeGear's server, nobody will see anything that you
post somewhere else.
Post by tbone
80% of these have nothing but a handful of spam.
The CodeGear servers are nearly spam-free. Spam posted through the
spammers' ISPs doesn't propagate to CodeGear's server, and spam posted
directly gets removed very quickly.

Furthermore, nearly all the newsgroups got new names. That was a couple
of years ago. If your ISP were on top of things, it would have started
carrying the new groups, but since nobody uses the groups through their
ISPs' servers, nobody complained to the ISPs that the groups were
inaccessible, so the ISPs never noticed.
Post by tbone
The one group with
more than a few hundred messages since 1/1/07 is called
"...non-technical" which didn't sound like it was of any interest.
The non-tech group can be very interesting, but it's not for technical
posts. The group you're interested in is borland.public.delphi.rtl.win32.
Post by tbone
So while it's indeed obvious I'm not looking on the CodeGear servers,
it's certainly not obvious exactly where that enticing newsgroup
content is located.
Post by Rob Kennedy
You might remember the KeyPressed or ReadKey functions. Try the versions
from Rudy Velthuis's Console.pas unit.
http://rvelthuis.de/programs/console.html
As I mentioned, I'm not at all familiar with any version of Delph.
You said you used to know Pascal. KeyPressed and ReadKey are not Delphi
functions. They were available in Turbo Pascal.
--
Rob
tbone
2007-05-23 13:54:10 UTC
Permalink
Minus the 'tude, there's a lot of good info here. Thanks.

I did know Pascal - on Vax/VMS, circa 1982. I've never used Turbo
Pascal.

Thanks for the help - it is appreciated.
tbone
Post by Rob Kennedy
Post by tbone
Post by Rob Kennedy
Of the 10 newsgroups with "Delphi" in their names that I read, I
estimate eight of them get messages every day. You're obviously not
looking on the CodeGear servers.
Since you didn't provide a URL, I located the CodeGear website.
To provide a URL, I would have had to use a search engine. You're just
as capable of asking Google for "codegear newsgroups" as I am.
Post by tbone
After
poking around a bit there, it seems that you're ultimately referring
to the borland.public.delphi... groups, which are available on my news
server.
Your ISP is doing you a disservice by providing them on its news server.
CodeGear's news servers do not accept submissions from outside sources;
you must post directly to CodeGear's own servers. Messages posted to
your ISP's server are not propagated for CodeGear's server, and since
all users read from CodeGear's server, nobody will see anything that you
post somewhere else.
Post by tbone
80% of these have nothing but a handful of spam.
The CodeGear servers are nearly spam-free. Spam posted through the
spammers' ISPs doesn't propagate to CodeGear's server, and spam posted
directly gets removed very quickly.
Furthermore, nearly all the newsgroups got new names. That was a couple
of years ago. If your ISP were on top of things, it would have started
carrying the new groups, but since nobody uses the groups through their
ISPs' servers, nobody complained to the ISPs that the groups were
inaccessible, so the ISPs never noticed.
Post by tbone
The one group with
more than a few hundred messages since 1/1/07 is called
"...non-technical" which didn't sound like it was of any interest.
The non-tech group can be very interesting, but it's not for technical
posts. The group you're interested in is borland.public.delphi.rtl.win32.
Post by tbone
So while it's indeed obvious I'm not looking on the CodeGear servers,
it's certainly not obvious exactly where that enticing newsgroup
content is located.
Post by Rob Kennedy
You might remember the KeyPressed or ReadKey functions. Try the versions
from Rudy Velthuis's Console.pas unit.
http://rvelthuis.de/programs/console.html
As I mentioned, I'm not at all familiar with any version of Delph.
You said you used to know Pascal. KeyPressed and ReadKey are not Delphi
functions. They were available in Turbo Pascal.
Henry Bartlett
2007-05-21 22:43:40 UTC
Permalink
Post by tbone
Since you didn't provide a URL, I located the CodeGear website. After
poking around a bit there, it seems that you're ultimately referring
to the borland.public.delphi... groups, which are available on my news
server.
NO! You cannot access the *genuine* borland.public.delphi... groups
via your normal news server. You *must* use the borland servers. See
http://www.hotkey.net.au/~hambar/habit/newsgroups.htm.
Post by tbone
80% of these have nothing but a handful of spam. The one group with
more than a few hundred messages since 1/1/07 is called
"...non-technical" which didn't sound like it was of any interest.
This is a perfect illustration of the problem of trying to access this
newsgroup via UseNet. There have been 87 posts to the borland
incarnation of this newsgroup in the last 24 hours, .

But borland.public.delphi.non-technical is not the appropriate group
for your query.

Try borland.public.delphi.language.delphi.win32 on the *borland*
servers (over 20 posts in the last 24 hours)


See you there.

--
Henry Bartlett
Delphi Links Page:
( http://www.hotkey.net.au/~hambar/habit/delflink.htm )
tbone
2007-05-23 13:56:31 UTC
Permalink
On Tue, 22 May 2007 08:43:40 +1000, "Henry Bartlett"
Post by Henry Bartlett
NO! You cannot access the *genuine* borland.public.delphi... groups
via your normal news server. You *must* use the borland servers. See
http://www.hotkey.net.au/~hambar/habit/newsgroups.htm.
I was unaware of this. I have now subscribed directly to borland's
newsgroups.
Post by Henry Bartlett
See you there.
Indeed!

Thanks
tbone

Terry Russell
2007-05-17 02:12:15 UTC
Permalink
Post by tbone
hmm... all the Delphi groups but this one are essentially silent.
Doesn't anyone use Delphi anymore?
I've been given a Delphi 4 project, and I'm new to Delphi (I used to
know Pascal). This program has many quirks, one of which is prompting
the user at one point to "press any key" (it's a console app and this
is after an error message at startup).
The Read procedure is used to read one character from the keyboard.
Trouble is, you can type any character you want, but the only thing
that works is the Enter key or control-C.
The help says that if you use Read with a Char var, it reads one
character, whereas with a string var, it reads up to a newline. My
experience here seems to indicate that it doesn't matter - it still
waits for a newline.
To wit: ----------------
procedure ShowHelp;
var aKey: char;
begin
writeln;
writeln;
... help messages displayed with writelns ...
writeln('Press any key...');
read(aKey); { doesn't return until you hit Enter}
{keys are echoed in console window }
end;
Any pointers? I know Delphi 4 is ancient but I don't have
authorization to upgrade to anything newer (yet).
A console app in delphi is still a win32 app , it needs windows to run
and won't run under pure dos.
As such there isn't a lot of support for consoles when you may as well
do it as a GUI.
Console i/o , mostly i , is treated differently to normal files.

there are a few crt tools like
http://www.zieglersoft.dk/public/crt32.asp?temp=974&language=uk
that give dos/pascal functionality


The following works, I am not saying it is the best, recommended or only way
just what
a quick grab from the junk heap turned up.

program Project1;
{$apptype console}
uses
windows,sysutils;

var Cin,Cout:Thandle;
function getstdhandles:boolean;
begin

cin:=getstdhandle(STD_INPUT_HANDLE);
cout:=getstdhandle(STD_OUTPUT_HANDLE);
result:= (cin <> INVALID_HANDLE_VALUE ) and
(cin <> INVALID_HANDLE_VALUE );
end;

function keypressed:boolean;
var inputrecords:DWORD;
var ir:_INPUT_RECORD;
var bufcount:DWORD;
begin
inputrecords:=0;
GetNumberOfConsoleInputEvents(cin,inputrecords);
result:=false;
if inputrecords<> 0 then
begin
peekConsoleinput( cin,ir,1,bufcount);
if bufcount <> 0 then
begin
if ir.EventType = KEY_EVENT then
begin
if ir.Event.KeyEvent.bKeyDown then
result:=true
else
flushconsoleinputbuffer(cin);
end
else
flushconsoleinputbuffer(cin);
end;
end
end;

function readkey:char;
var ir:_INPUT_RECORD;
var bufcount:DWORD;
begin
if keypressed then
peekConsoleinput( cin,ir,1,bufcount);
if bufcount>0
then result:= ir.Event.KeyEvent.AsciiChar
else result:=char(0);
flushconsoleinputbuffer(cin);
end;

procedure getkey;
var aKey: char;
begin
writeln('enter a key');
writeln;
akey:=#0;
while akey <> #27 do //esc
begin
while not keypressed do;
akey:=readkey;
writeln( inttostr(byte(akey)) + akey );
end;
end;


begin
getstdhandles;
getkey;
end.
Marc Rohloff
2007-05-17 13:09:23 UTC
Permalink
Post by tbone
Any pointers? I know Delphi 4 is ancient but I don't have
authorization to upgrade to anything newer (yet).
You can get a free version of Delphi 2006 called Turbo Explorer here:
http://www.turboexplorer.com/

It is a cut down evrsion but might well do eevrything that you need.

--
Marc
tbone
2007-05-21 19:13:44 UTC
Permalink
Thanks...I'll have a look.
tbone

On 17 May 2007 06:09:23 -0700, Marc Rohloff
Post by Marc Rohloff
Post by tbone
Any pointers? I know Delphi 4 is ancient but I don't have
authorization to upgrade to anything newer (yet).
http://www.turboexplorer.com/
It is a cut down evrsion but might well do eevrything that you need.
Henry Bartlett
2007-05-18 00:16:10 UTC
Permalink
Post by tbone
hmm... all the Delphi groups but this one are essentially silent.
Doesn't anyone use Delphi anymore?
Check out
http://www.hotkey.net.au/~hambar/habit/delflink.htm#newsgroups
and
http://www.hotkey.net.au/~hambar/habit/newsgroups.htm

Of the Usenet groups, comp.lang.pascal.delphi.misc also has new posts
nearly every day, but, IMHO, the best group to ask this type of
question in is borland.public.delphi.language.delphi.win32
(*NOTE: make sure that you use the group on the Borland servers - (see
http://info.borland.com/newsgroups/ng_delphi.html.)
and not a similarly named group on a Usenet server*)

I cannot improve on the advice on the solution to your problem that
has been given by others

--
Henry Bartlett
Delphi Links Page:
( http://www.hotkey.net.au/~hambar/habit/delflink.htm )
Continue reading on narkive:
Loading...