How can I interrupt a loop with a key press in C++ on OS X?
<p>I would like to do the following in a command-line application on OS X:</p>

<pre><code>while (true) {
    // do some task
    if (user_has_pressed('x')) {
        // break out of the loop and do something different
    }
}
</code></pre>

<p>For clarity, I don't want the program to block waiting for user input. I'm running a numerical simulation that takes many hours to run, and I want to press a key to print detailed statistics on its progress, or interrupt it and change parameters, etc.</p>

<p>There are some existing <a href="https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed">similar</a> <a href="https://stackoverflow.com/questions/9547868/is-there-a-way-to-get-user-input-without-pressing-the-enter-key">questions</a>, but the answers either suggest a windows-only <code>getch</code> function, or they switch the terminal into a different input mode. I don't want to do that, because I need to retain the ability to interrupt with <code>ctrl-c</code> without messing up the terminal.</p>

<p>I don't want to build a Cocoa application, and I don't care about being cross-platform. I'm just looking for the simplest quick-and-dirty way to do it in a command line app that will only ever be run on my own machine. </p>

<p>I guess one option is to use ncurses. From a brief bit of reading it seems like a heavier option than I'd like - but if somebody would post a simple minimal example that accomplishes the above task that would be really helpful.</p>