I would like to do the following in a command line application in OS X:
while (true) {
// do some task
if (user_has_pressed('x')) {
// break out of the loop and do something different
}
}
For clarity, I do not want the program to block waiting for user input. I run a numerical simulation that takes many hours, and I want to press a key to print detailed statistics about its progress or interrupt it, change parameters, etc.
There are some similar questions that exist , but the answers are either offered only for Windows getch, or they switch the terminal to a different input mode. I do not want to do this because I need to save the possibility of interrupting with ctrl-c, without ruining the terminal.
I do not want to create a Cocoa application, and I do not need to be cross-platform. I'm just looking for the easiest quick and dirty way to do this in a command line application that will only run on my own machine.
I assume one option is to use ncurses. From a brief reading, this seems like a more difficult option than we would like - but if someone publishes a simple minimal example that will accomplish the above task, which will be really useful.
source
share