For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > December 2004 > Getting main loop to recognize escap key?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Getting main loop to recognize escap key?
James Johnson

2004-12-14, 9:00 pm


I have a main.cpp running in a continuous loop. I have two files open and I've been stopping the program with control-c. Is this the correct way to stop a unix program? I'd
like to get the loop to see the escape key (without polling for it). Or, how can I respond to control-c in the program so that I can close files etc.?

thanks,
Jim


Måns Rullgård

2004-12-14, 9:01 pm

James Johnson <jjjhnson2-1779231@yahoo.com> writes:

> I have a main.cpp running in a continuous loop. I have two files open
> and I've been stopping the program with control-c. Is this the correct
> way to stop a unix program? I'd like to get the loop to see the escape
> key (without polling for it). Or, how can I respond to control-c in
> the program so that I can close files etc.?


man sigaction

Ctrl-C generates a SIGINT.

--
Måns Rullgård
mru@inprovide.com
Wayne C. Morris

2004-12-14, 9:01 pm

In article <yw1x4qiofvs2.fsf@ford.inprovide.com>,
Måns Rullgård <mru@inprovide.com> wrote:

> James Johnson <jjjhnson2-1779231@yahoo.com> writes:
>
>
> man sigaction
>
> Ctrl-C generates a SIGINT.


Not necessarily. That seems to be the default on Linux and Mac OS X, but
many of the Unix systems I've worked on use DEL as the default intr key.
To change it to control-C, add "stty intr '^C'" to your login script.
Måns Rullgård

2004-12-14, 9:01 pm

"Wayne C. Morris" <wayne.morris@this.is.invalid> writes:

> In article <yw1x4qiofvs2.fsf@ford.inprovide.com>,
> Måns Rullgård <mru@inprovide.com> wrote:
>
>
> Not necessarily. That seems to be the default on Linux and Mac OS X, but
> many of the Unix systems I've worked on use DEL as the default intr key.
> To change it to control-C, add "stty intr '^C'" to your login script.


If the OP had such a system, he wouldn't be using ctrl-c to stop his
program.

--
Måns Rullgård
mru@inprovide.com
James Johnson

2004-12-15, 3:59 pm

On Tue, 14 Dec 2004 21:51:41 +0100, Måns Rullgård <mru@inprovide.com> wrote:

>James Johnson <jjjhnson2-1779231@yahoo.com> writes:
>
>
>man sigaction
>
>Ctrl-C generates a SIGINT.


I got control-c to work great using SIGINT. But I'm still wondering if you can get a unix C main loop to wake up on hitting a key on the keyboard?

Jim

Måns Rullgård

2004-12-15, 3:59 pm

James Johnson <jjjhnson2-1779231@yahoo.com> writes:

> On Tue, 14 Dec 2004 21:51:41 +0100, Måns Rullgård <mru@inprovide.com> wrote:
>
>
> I got control-c to work great using SIGINT. But I'm still wondering
> if you can get a unix C main loop to wake up on hitting a key on the
> keyboard?


Sure, just twiddle the mode of your tty properly, and use select().

--
Måns Rullgård
mru@inprovide.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com