c - How can I detect different control signals? -
in program, want detect when user presses ctrl + a,b,c,d,e... different actions each character. have :
int main(){ signal(sigint, sighandler); while(1) { sleep(1); } return(0); } void sighandler(int signum){ printf("caught signal %d, coming out...\n", signum); exit(1); }
i'd control ctrl+chars ctrl+c being detected, how can detect other characters too?
no. ctrl+c not "control signal".
in posix systems, terminals have 3 keypresses cause kernel send signal process reading terminal: ctrl+c interrupt (sigint
), ctrl+\ quit (sigquit
), ctrl+z suspend (sigtstp
). can override keypresses ctrl+key combination using posix termios interface -- remember, there 3 (signals , possible keypresses).
it more common use termios interface (or equivalently stty
command in scripts) put terminal raw mode, in case application gets keypresses not consumed kernel or graphical user interface (key combinations reserved stuff switching terminals, closing windows, , on).
the portable way use common library called curses. common implementation (that has few goodies compared plain curses) ncurses -- no need go link download , compile sources; library packaged distribution, can find in standard software repositories. windows, there pdcurses; mingw compiler, can use ncurses in windows, too.
do not fooled ages of various versions of library (although recommend ncurses 6 if can utilize it); curses interface stable, , been used long time. ncurses add useful stuff, , actively maintained still.
Comments
Post a Comment