How can I "peek" at the keyboard without suspending?

J

Jeffrey Hiess

Something like getchar() doesn't work, since it "waits" for input.


#include <stdio.h>
while (1)
{
if (getchar()) /* break if any key is pressed */
{
break;
}

/* lot's of other code goes here */
}
 
J

Jeffrey Hiess

Ever considered reading the FAQ *before* posting?

Dan

Did I miss something? Which FAQ? Please expound. I'm posting from
Google Groups.

On the topic of the original post, I am hesitant to write platform
specific code to avoid suspending. Instead, I was pondering the idea
of forking a sub process which would call getchar() (and suspend) and
then main process could monitor the sub process for a signal to take
action. My multi-threading coding skills are a bit rusty, but it seems
like this would work.
 
K

Keith Thompson

(e-mail address removed) (Dan Pop) wrote in message


Did I miss something? Which FAQ? Please expound. I'm posting from
Google Groups.

On the topic of the original post, I am hesitant to write platform
specific code to avoid suspending. Instead, I was pondering the idea
of forking a sub process which would call getchar() (and suspend) and
then main process could monitor the sub process for a signal to take
action. My multi-threading coding skills are a bit rusty, but it seems
like this would work.

Forking and monitoring other processes are at least as system-specific
as checking for pending keyboard input.
 
M

Morris Dovey

Jeffrey said:
On the topic of the original post, I am hesitant to write platform
specific code to avoid suspending. Instead, I was pondering the idea
of forking a sub process which would call getchar() (and suspend) and
then main process could monitor the sub process for a signal to take
action. My multi-threading coding skills are a bit rusty, but it seems
like this would work.

Jeffrey...

It sounds like might be a good place to
ask this question. If you have access to "Advanced Programming in
the UNIX Environment" by Richard Stevens, you might find it
helpful. You might find http://www.iedu.com/mrd/c/getch.c
(written with help from APUE to run under Linux) interesting.

Note that your code will (necesarily) be less portable than a
standard-compliant program that uses getchar() - the price for
being just a bit chummier with the operating system.
 
G

Gordon Burditt

On the topic of the original post, I am hesitant to write platform
specific code to avoid suspending.

You have no choice. ANSI C does not provide non-blocking I/O.
Instead, I was pondering the idea
of forking
^^^^^^^
PLATFORM-SPECIFIC
a sub process
^^^^^^^^^^^
PLATFORM-SPECIFIC
which would call getchar() (and suspend) and
then main process could monitor
^^^^^^^
If this is anything but setting up a signal handler, this is PLATFORM-SPECIFIC
the sub process
^^^^^^^^^^^
PLATFORM-SPECIFIC
for a signal to take
^^^^^^^^^^^^
sending a signal to another process is PLATFORM-SPECIFIC.
action. My multi-threading coding skills are a bit rusty, but it seems
^^^^^^^^^^^^^^^
Multi-threading except for the skimpy support for signal handlers in C
is PLATFORM-SPECIFIC.
like this would work.

Gordon L. Burditt
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top