Hi,
I'm trying to write a while loop that is able to recognise and act on keyboard presses without pausing to wait for an input (unlike getchar()).
At the moment I'm just trying to detect any keypress and exit the loop by changing an integer, printing out the recorded keypress on the way out. Later I'd like to recognise the keypress as part of conditional statements and keep looping.
Here is the code that I've got so far, it has been modified from an example. Currently the loop just keeps running indefinately, regardless of what I press.
Some extra information that may be important: this code is a small addition to a large working example (which I am modifying) that runs on a remote embedded PC, I've omitted about 450 lines (including lots of headers) for this snippet. Also, it may be useful to know that I'm accessing the PC via Putty , a telnet / SSH client.
Thanks for reading, any help appreciated,Code:#include <stdio.h> #include <err.h> #include <curses.h> /* for ERR */ int main(){ char chr; /* Keyboard character */ int done = 0; /* while condition */ while (done != 1){ printf(". "); /* show that we are looping */ usleep(5000); /* wait a moment */ if ((chr = getch()) != ERR) done = 1; /* if anything has been pressed leave the loop*/ } printf(" chr = %c\n", chr); /* show which key was pressed */ }
Ad