Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > keypresses and signal handling

Reply
Thread Tools

keypresses and signal handling

 
 
Rich
Guest
Posts: n/a
 
      09-25-2010
Hi,

In my program I want it so when the users presses the space bar (or
any other key if it is easier) they get a printf("hello, I am still
processing your request please wait") while the program is doing its
work. I added a signal handler to catch Ctrl-C and that works great
(i.e the program gracefully exits instead of crashes). Is there some
other signal I can use to do what I want?

Thanks,
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-25-2010
On 9/25/2010 4:06 PM, Rich wrote:
> Hi,
>
> In my program I want it so when the users presses the space bar (or
> any other key if it is easier) they get a printf("hello, I am still
> processing your request please wait") while the program is doing its
> work. I added a signal handler to catch Ctrl-C and that works great
> (i.e the program gracefully exits instead of crashes). Is there some
> other signal I can use to do what I want?


C itself has no such capability, although the systems C runs on
may offer something. Check your system's documentation.

By the way, you almost certainly do *not* want to call printf()
in a signal handler, nor any other library function that your system's
documentation does not specifically describe as "async signal safe" or
something along those lines. Trouble can occur (usually, does occur)
if the signal arrives while the program is executing printf(), or some
other function (malloc(), maybe?) that printf() calls internally. If
you're half-way through a realloc() call, go into the signal handler
while realloc() has flour scattered all over the kitchen and the fridge
door hanging open, and then try to call malloc() before anybody's
had a chance to tidy up -- well, the outcome is seldom a happy one.

An alternative you might consider is to display some kind of
animated progress indicator: A number that starts at 417 and counts
down to zero, or a little twirling baton, or something like that. See
Question 19.3 on the comp.lang.c Frequently Asked Questions (FAQ) page
at <http://www.c-faq.com/>.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      09-25-2010
On 2010-09-25, Rich <(E-Mail Removed)> wrote:
> Hi,
>
> In my program I want it so when the users presses the space bar (or
> any other key if it is easier) they get a printf("hello, I am still
> processing your request please wait") while the program is doing its
> work. I added a signal handler to catch Ctrl-C and that works great
> (i.e the program gracefully exits instead of crashes). Is there some
> other signal I can use to do what I want?


Not in general, no. This is a FAQ. You may find it useful to read
the newsgroup's FAQ:

http://c-faq.com/

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      09-25-2010
Eric Sosman wrote:

> By the way, you almost certainly do *not* want to call printf()
> in a signal handler, nor any other library function that your system's
> documentation does not specifically describe as "async signal safe" or
> something along those lines. Trouble can occur (usually, does occur)
> if the signal arrives while the program is executing printf(), or some
> other function (malloc(), maybe?) that printf() calls internally. If
> you're half-way through a realloc() call, go into the signal handler
> while realloc() has flour scattered all over the kitchen and the fridge
> door hanging open, and then try to call malloc() before anybody's
> had a chance to tidy up -- well, the outcome is seldom a happy one.


I would claim the exception when you're testing it to see if it's ever
being used. I get stung on related issues regularly.


$ gcc -Wall -Wextra u4.c -o out
$ ./out
42
[file u4.c, line 14]: hey, x=-1207183232
$ cat u4.c
// debugging macros so we can pin down message provenance at a glance
#define WHERESTR "[file %s, line %d]: "
#define WHEREARG __FILE__, __LINE__
#define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__)
#define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG,
__VA_ARGS__)

#include <stdio.h>

int main (void)
{
int x; //never gave it a value

printf("42\n");
DEBUGPRINT("hey, x=%d\n", x);
return 0;
}


// gcc -Wall -Wextra -dD -E u4.c >text1.txt
// gcc -Wall -Wextra u4.c -o out
$

Are __VA_ARGS__ and _fmt defined in a standard way?
--
Uno
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      09-26-2010
Richard wrote:
> Seebs <(E-Mail Removed)> writes:

the newsgroup's FAQ:

>> http://c-faq.com/
>>
>> -s

>
> Why not answer the bloody question instead of passing him off to a
> massive FAQ?
>
>


seebs 101: in the last case, help.

He takes douchebag to a new. unemployable, English level.
--
Uno
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-26-2010
On 09/26/10 04:14 PM, Richard wrote:
> Seebs<(E-Mail Removed)> writes:
>
>> On 2010-09-25, Rich<(E-Mail Removed)> wrote:
>>> Hi,
>>>
>>> In my program I want it so when the users presses the space bar (or
>>> any other key if it is easier) they get a printf("hello, I am still
>>> processing your request please wait") while the program is doing its
>>> work. I added a signal handler to catch Ctrl-C and that works great
>>> (i.e the program gracefully exits instead of crashes). Is there some
>>> other signal I can use to do what I want?

>>
>> Not in general, no. This is a FAQ. You may find it useful to read
>> the newsgroup's FAQ:
>>
>> http://c-faq.com/
>>
>> -s

>
> Why not answer the bloody question instead of passing him off to a
> massive FAQ?


Why don't you? Oh, I forgot you're only here to snipe.

--
Ian Collins
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      09-26-2010
On Sun, 26 Sep 2010 05:14:23 +0200, Richard <(E-Mail Removed)> wrote:

>Seebs <(E-Mail Removed)> writes:
>
>> On 2010-09-25, Rich <(E-Mail Removed)> wrote:
>>> Hi,
>>>
>>> In my program I want it so when the users presses the space bar (or
>>> any other key if it is easier) they get a printf("hello, I am still
>>> processing your request please wait") while the program is doing its
>>> work. I added a signal handler to catch Ctrl-C and that works great
>>> (i.e the program gracefully exits instead of crashes). Is there some
>>> other signal I can use to do what I want?

>>
>> Not in general, no. This is a FAQ. You may find it useful to read
>> the newsgroup's FAQ:
>>
>> http://c-faq.com/
>>
>> -s

>
>Why not answer the bloody question instead of passing him off to a
>massive FAQ?


Probably because he doesn't know the specific FAQ that pertains to the
question and he refuses to do the OP's searching for him.

Perhaps also because a search of the linear FAQ index:
http://c-faq.com/questions.html for "keypress" or "signal" or
"handling" yields no results.

 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      09-26-2010
Ian Collins wrote:
> On 09/26/10 04:14 PM, Richard wrote:


>> Why not answer the bloody question instead of passing him off to a
>> massive FAQ?

>
> Why don't you? Oh, I forgot you're only here to snipe.
>



So Ian, are you the second english information non-provider here on this
thread, because unlike seebs, I like you.

Crap Almighty, I think OP is fishing for something that begins with SEG
in caps.

You didn't forget, and you are here to snipe, which has a regional
interpretation.
--
Uno
 
Reply With Quote
 
luserXtrog
Guest
Posts: n/a
 
      09-26-2010
On Sep 25, 3:06*pm, Rich <(E-Mail Removed)> wrote:
> Hi,
>
> In my program I want it so when the users presses the space bar (or
> any other key if it is easier) they get a printf("hello, I am still
> processing your request please wait") while the program is doing its
> work. *I added a signal handler to catch Ctrl-C and that works great
> (i.e the program gracefully exits instead of crashes). *Is there some
> other signal I can use to do what I want?
>
> Thanks,


If you've got POSIX, this might work.
You send a SIGINT with ^c (as you know)
and a SIGQUIT with ^\.



#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <termios.h>
#include <unistd.h>

struct termios ts;

void on_int (int sig) {
puts("bye");
tcsetattr(fileno(stdin), TCSANOW, &ts);
exit(0);
}

void on_quit (int sig) {
puts("hello, I am still processing your request please wait");
}

int main() {

{ /* Enable other signal keys */
struct termios tsb;
tcgetattr(fileno(stdin), &ts);
tsb = ts;
tsb.c_lflag = ISIG;
tcsetattr(fileno(stdin), TCSANOW, &tsb);
}

{ /* Install Signal Handlers */
signal(SIGINT, on_int);
signal(SIGQUIT, on_quit);
}

while(1) {
usleep(500);
}
return 0;
}

/* eof */
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      09-26-2010
On Sep 25, 10:06*pm, Rich <(E-Mail Removed)> wrote:
>
> In my program I want it so when the users presses the space bar (or
> any other key if it is easier) they get a printf("hello, I am still
> processing your request please wait") while the program is doing its
> work. *I added a signal handler to catch Ctrl-C and that works great
> (i.e the program gracefully exits instead of crashes). *Is there some
> other signal I can use to do what I want?
>

kbhit() does what you want (I think, it's ages since I used it). It is
available on DOS.

just write if(khit()) printf("hello, I am still processing your
request please wait") somewhere in u=your inner loop. You might also
need to read the character.

If you don't have kbhit() this page gives an implementation for linux.

http://www.linux-sxs.org/programming/kbhit.html


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Respond to keypresses on top of a paragraph mflll@wiu.edu Javascript 1 04-14-2007 04:48 PM
intercepting keypresses, mouse movements, joystick movements. bryan rasmussen Python 2 10-09-2006 07:11 PM
How do I scan for keypresses in console ruby? Myrddin Emrys Ruby 1 07-24-2006 06:04 PM
Re: JTable in Applet doesn't receive keypresses under Win2000 (XP is Ok) Dag Sunde Java 0 12-30-2004 04:16 PM
Detecting multiple keypresses kstahl C Programming 7 05-13-2004 05:43 PM



Advertisments