views:

42

answers:

1

How can I execute a command when user press a key in a command-line C application? Is it possible when window isn't focused

A: 

Depends on the program itself, you could do either of those:

  1. block on unbuffered getc you get the key strokes as they come and not when users hits return.

  2. create some sort of event loop, using select/epoll or event framework like (libevent/libev) and get a callback when ever a user hits a key.

  3. use a toolkit like ncurses which provides a pseudo graphical command line interface and an event loop.

  4. if the keys you're interesting in capturing are things like CTRL+C, you need signal handlers.

miedwar