Using sigint ctrl-c

okay, so I use sighandler to interpret some signal, for that it is ctrl-c, so when ctrl-c is typed, some action will be taken, and everything is fine and dandy, but what I really need is this happened without ^ c appearing on input / output

for example let's say i have this code

#include <stdio.h> #include <stdlib.h> #include <signal.h> void siginthandler(int param) { printf("User pressed Ctrl+C\n"); exit(1); } int main() { signal(SIGINT, siginthandler); while(1); return 0; } 

the output will be

^ CUser pressed Ctrl + C

how can i get it just

User pressed Ctrl + C?

+6
c copy-paste sigint
source share
3 answers

You can achieve this effect by using the "noecho ()" call to the curses proxy library, but you must also agree to the use (and / or use) of curses proxy processing ...

 #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <curses.h> void siginthandler(int param) { endwin(); printf("User pressed Ctrl+C\n"); exit(1); } int main() { initscr(); noecho(); signal(SIGINT, siginthandler); while(1); return 0; } 
+3
source share

Most likely, this is not your program. Most likely, these are terminal drivers, in which case you should look at stty with the ctlecho or echoctl .

You can use stty -a to get all the current settings for your terminal. For a programmatic interface to these elements, termios is the way to go.

+1
source share

As others have said, such things fall under the control of your terminal driver and are usually done using the curses library. However, here is a hack that will probably help you:

 #include <stdio.h> #include <stdlib.h> #include <signal.h> void siginthandler(int param) { printf("User pressed Ctrl+C\n"); system("stty echo"); exit(1); } int main() { system("stty -echo"); signal(SIGINT, siginthandler); while(1); return 0; } 
0
source share

All Articles