how to "wait for any key"

  • Thread starter Alexander Malkis
  • Start date
A

Alexander Malkis

What is the most portable way of "waiting for any key", i.e. the user
who runs a program from the console should observe the following:

- the program stops running
- as soon as the user presses any keyboard key (alphanum/Enter), the
program continues.

Thanks a lot,
Alex.
 
T

Tydr Schnubbis

Alexander said:
What is the most portable way of "waiting for any key", i.e. the user
who runs a program from the console should observe the following:

- the program stops running
- as soon as the user presses any keyboard key (alphanum/Enter), the
program continues.

Thanks a lot,
Alex.
There is no portable way. The closest you can get is using
gethcar(), but that will only return when the user presses enter, and
other keys pressed will be echoed to the console.

You could possibly use some kind of portable I/O library to achieve
portability across at least some platforms. Or you could do your
project in a language that has such functions standardized,
can't think of any right now.

Tor
 
O

osmium

Alexander said:
What is the most portable way of "waiting for any key", i.e. the user
who runs a program from the console should observe the following:

- the program stops running
- as soon as the user presses any keyboard key (alphanum/Enter), the
program continues.

Isolate the function you need in a function of your own making. Have that
function (kind of an emulator) do whatever is necessary to implement the
logic you need as each new platform comes to light. Do a *usenet* search
for conio and ncurses for a good start on what you need. You will need to
use the pre processor to decide which platform is being compiled for if you
insist on automating the process. I wouldn't automate it unless there was
some cash money involved, that is, I would compile on an ad hoc basis.

Someone may have already done this and you could conceivably make a lucky
hit on Usenet. It is certainly something the world needs.
 
D

Derk Gwen

# What is the most portable way of "waiting for any key", i.e. the user
# who runs a program from the console should observe the following:

(1) No portable way.
(2) Confuses the hell out of some people. (Where's the ANY key?)
(3) Doesn't invariably work. SHIFT or CTRL, for example, might not send
any codes to the computer. ESC or CTRL-C can break your program.

Just say "Hit enter (or return)" and use fgets. Clear, simple, direct,
portable, and easy enough for nearly everyone.
 
A

Alexander Malkis

fgets(...) is a good idea! Thank you.
I was thinking of getch() from ncurses, but it's not standard.

Derk said:
# What is the most portable way of "waiting for any key", i.e. the user
# who runs a program from the console should observe the following:

(1) No portable way.
(2) Confuses the hell out of some people. (Where's the ANY key?)
(3) Doesn't invariably work. SHIFT or CTRL, for example, might not send
any codes to the computer. ESC or CTRL-C can break your program.

Just say "Hit enter (or return)" and use fgets. Clear, simple, direct,
portable, and easy enough for nearly everyone.


--

Best regards,
Alex.

PS. My real email is formed by deleteing the letter combination
"loeschedies" from the email address given.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top