By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 */
}
Nov 13 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In article <f5**************************@posting.google.com >, Jeffrey Hiess wrote:
Something like getchar() doesn't work, since it "waits" for input.

Read this FAQ:

http://www.eskimo.com/~scs/C-faq/q19.1.html
--
Andreas Kähäri
Nov 13 '05 #2

P: n/a
Andreas Kahari <ak*******@freeshell.org> wrote:
In article <f5**************************@posting.google.com >, Jeffrey Hiess wrote:
Something like getchar() doesn't work, since it "waits" for input.

Read this FAQ:

http://www.eskimo.com/~scs/C-faq/q19.1.html


To OP: in future please cross-post your message instead of
multi-posting. Your question has already been answered in
alt.comp.lang.learn.c-c++. Thank you.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3

P: n/a
In <f5**************************@posting.google.com > je***********@honeywell.com (Jeffrey Hiess) writes:
Something like getchar() doesn't work, since it "waits" for input.


Ever considered reading the FAQ *before* posting?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4

P: n/a
Da*****@cern.ch (Dan Pop) wrote in message news:<bq**********@sunnews.cern.ch>...
In <f5**************************@posting.google.com > je***********@honeywell.com (Jeffrey Hiess) writes:
Something like getchar() doesn't work, since it "waits" for input.


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.
Nov 13 '05 #5

P: n/a
je***********@honeywell.com (Jeffrey Hiess) writes:
Da*****@cern.ch (Dan Pop) wrote in message
news:<bq**********@sunnews.cern.ch>...
In <f5**************************@posting.google.com >
je***********@honeywell.com (Jeffrey Hiess) writes:
Something like getchar() doesn't work, since it "waits" for input.


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.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 13 '05 #6

P: n/a
Jeffrey Hiess wrote:
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 news:comp.unix.programmer 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.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 13 '05 #7

P: n/a
>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-SPECIFICa sub process ^^^^^^^^^^^
PLATFORM-SPECIFICwhich would call getchar() (and suspend) and
then main process could monitor ^^^^^^^
If this is anything but setting up a signal handler, this is PLATFORM-SPECIFICthe sub process ^^^^^^^^^^^
PLATFORM-SPECIFICfor 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
Nov 13 '05 #8

P: n/a
je***********@honeywell.com (Jeffrey Hiess) wrote:
Did I miss something? Which FAQ? Please expound. I'm posting from
Google Groups.


That is frequently an explanation, but never an excuse.

Richard
Nov 13 '05 #9

P: n/a
Jeffrey Hiess <je***********@honeywell.com> spoke thus:
Did I miss something? Which FAQ? Please expound. I'm posting from
Google Groups.


Since no one else seemed to be clear, yes, you did miss something -
the FAQ located at http://www.eskimo.com/~scs/C-faq/top.html.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #10

P: n/a
Christopher Benson-Manica wrote:
Jeffrey Hiess <je***********@honeywell.com> spoke thus:
Did I miss something? Which FAQ? Please expound. I'm posting from
Google Groups.


Since no one else seemed to be clear, yes, you did miss something -
the FAQ located at http://www.eskimo.com/~scs/C-faq/top.html.


There seems to be no downloadable version of the full latest FAQ.

Ian

Nov 13 '05 #11

P: n/a
Ian Bell <ia*@ruffrecordsDOTworldonline.co.uk> wrote:
Christopher Benson-Manica wrote:
Jeffrey Hiess <je***********@honeywell.com> spoke thus:
Did I miss something? Which FAQ? Please expound. I'm posting from
Google Groups.


Since no one else seemed to be clear, yes, you did miss something -
the FAQ located at http://www.eskimo.com/~scs/C-faq/top.html.


There seems to be no downloadable version of the full latest FAQ.


Follow the link on http://www.eskimo.com/~scs/C-faq/top.html to
http://www.eskimo.com/~scs/C-faq/versions.html and from there to
ftp://ftp.eskimo.com/u/s/scs/C-faq/faq.Z.

Or try: http://www.faqs.org/faqs/C-faq/faq/index.html (~280K).

HTH ;)

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.angelfire.com/ms3/bchambl...me_to_clc.html
clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...acllc-c++.html
Nov 13 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.