Command-line C & keyboard shortcut (in Mac OS)
How can I exec开发者_C百科ute a command when user press a key in a command-line C application? Is it possible when window isn't focused
Depends on the program itself, you could do either of those:
block on unbuffered getc you get the key strokes as they come and not when users hits return.
create some sort of event loop, using select/epoll or event framework like (libevent/libev) and get a callback whenever a user hits a key.
use a toolkit like ncurses which provides a pseudo graphical command line interface and an event loop.
if the keys you're interesting in capturing are things like CTRL+C, you need signal handlers.
精彩评论