1

I have a problem with finding an appropriate function. Now, the loop that I'm speaking about looks like this:

do
{ cameras[0].GrabOne(5000, ptrGrabResult);
Pyon::DisplayImage(1, ptrGrabResult);
::Sleep(1000);
} while( cin.get() != ' ' );

It is executed constantly only when i hold enter key.

CherryCola
  • 27
  • 1
  • 5
  • Not quite a duplicate, but [this answer](http://stackoverflow.com/questions/11778115/beep-sound-till-any-input/11778620#11778620) may help. Worth nothing however, that I am not an experienced winodws programmer, and hopefully someone will be able to give a better recommendation. The key functions in my answer are [`_kbhit`](http://msdn.microsoft.com/en-us/library/58w7c94c%28v=vs.80%29.aspx) and [`_getch`](http://msdn.microsoft.com/en-us/library/078sfkak%28v=vs.80%29.aspx). – BoBTFish Jan 13 '14 at 11:52
  • Does it loop while holding non-space keys or just enter? It looks like it's waiting for input. – keyser Jan 13 '14 at 11:52
  • Just enter. I'll check the proposed functions and let you know if it works. – CherryCola Jan 13 '14 at 11:59
  • Better use some threading : a main thread listening for keyboard inputs, and a second one with while(!isStopRequested){do} , isStopRequested being a flag accessible from the main thread. – lucasg Jan 13 '14 at 12:08
  • @georgesl I don't think that is necessary, at least in this small example. (Although it may be an appropriate choice to have a separate input thread for a large application). But I think recommending threads, especially with shared data, to someone who appears to be a relative beginner, may not be a good idea. Also, it doesn't solve that issue that `std::cin` is generally line buffered (doesn't receive anything until the user hits enter). – BoBTFish Jan 13 '14 at 12:14
  • @BoBTFish : the issue with non-blocking calls like _getch() is that the user can type Enter and still not terminating the program, if the thread is in sleep mode and not in the while conditional . That's why a timer is needed here to properly end the loop. – lucasg Jan 13 '14 at 12:50
  • @georgesl You mean it may still sleep for up to a second after the user hit space? From the code, that seems to be the desired behaviour. If the requirement is that an image is displayed every second, but the application will finish immediately when space is hit, then that is a different question, although can still be done all on one thread if desired. – BoBTFish Jan 13 '14 at 13:09
  • @BoBTFish : my bad, I thought kbhit() and _getch() were less clever than they are. kbhit() is more than a simple lookup on the keyboard state since it also caches the last key hit between two calls. That way you can always terminate the loop, even if the keystroke happens during the processing part of the loop. – lucasg Jan 13 '14 at 13:41
  • It works! Thank all of you for help :) – CherryCola Jan 14 '14 at 09:59

0 Answers0