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.

The while loop keeps printing '. ' so it isn't blocking. I assume that means no-delay is true (though I haven't set this explicitly I believe that it is the default).

If i press enter I get a line break in the output (i.e. .........[\n]......) but the loop doesn't exit. It's the same with any character, it appears in the console but doesn't affect the loop behaviour.

Thanks,

Ad

12-20-2010

Ad_robot

Hi,

I used ERR as this seemed to have been used in the code I've been using as an example. I believed that the statement would return ERR if nothing had been pressed. Is this not a good choice?

Btw, I have tried the following without success, the loop still doesn't exit.

Quote:

if ((chr = getch()) == 'a') done = 1;

Thanks,

Ad

12-20-2010

tabstop

If your char's are unsigned, and ERR is negative (as I suspect it is), then your comparison will never be true. What if you make chr an int instead of a char?

12-20-2010

GReaper

You wait a total of 5 seconds. Your console maynot catch the keyboard hit.

12-20-2010

Ad_robot

Thanks for the suggestions.

tabstop I've made the chr an integer but I still get the same response.

Sipher I've reduced usleep to 50 and also taken it out completely but the same response continues. Also, I think usleep(5000) is a wait of 50ms (that seems to be how often the dots appear).

Ad

12-20-2010

GReaper

Oh, sorry. I confused usleep with Sleep... :D

12-20-2010

Ad_robot

Btw, I just checked and ERR is '-1'.

I put this in my while loop

Quote:

printf("\n. %d",getch());

But even if I hold down a key and make usleep very small, getch = '-1'. Interesting... it seems that my keystrokes aren't being detected by getch?

Could this be something to do with my remote interface? It seems to work on the demo code that shipped with the system, and that used getch for the input.

12-20-2010

Ad_robot

Aargh, but getchar() detects keys fine! So it's not the remote interface...

But even if I hold down a key and make usleep very small, getch = '-1'. Interesting... it seems that my keystrokes aren't being detected by getch?

Could this be something to do with my remote interface? It seems to work on the demo code that shipped with the system, and that used getch for the input.

getch may well be polling the keyboard at the other end and not accepting the keys from PuTTY. (I don't remember, if I ever knew, how those two interrelate.) The easy way to check would be to run this on your box and see what happens (or send someone over there to push buttons on that machine, if possible).

12-20-2010

Ad_robot

tabstop,

Unfortunately the other system doesn't have a keyboard, mouse or display, it's an embedded platform connected to some sensors and actuators.

The strange thing is that getchar() does detect the keypresses from my system, so one would assume that getch() would work the same. It seems not.

I didn't mention that the remote system is running Linux and I'm running Windows. This shouldn't be a problem thought should it?

Back to reading the ncurses guide...

12-20-2010

anduril462

Your code needs some curses requirements and you need to set the mode to cbreak. Here's my version with a few comments: