I have tried to use the getch() function to meet my requirement but it did not work. What am I supposed to do if I want to call a function in an infinite for loop only when I press some key otherwise the loop keeps on running without calling the function?
Asked
Active
Viewed 265 times
-2
-
How long is the duration of a keypress? As a keypress can span several loop cycles... – CodeAngry Dec 05 '15 at 17:59
-
2Please post some code so we have a reference point to work from. – Aidan Gomez Dec 05 '15 at 17:59
-
2Your (too broad) question is very similar to [this](http://stackoverflow.com/q/34108024/841108) one, and [my](http://stackoverflow.com/a/34108064/841108) answer should be relevant here. – Basile Starynkevitch Dec 05 '15 at 18:01
-
Do you want to have a loop running and then execute a function when a key is pressed? Or do you want your program to wait and continue after a key press? – Thomas Matthews Dec 05 '15 at 18:03
-
Keypress detection is platform specific. What platform (computer and OS) are you targeting? – Thomas Matthews Dec 05 '15 at 18:04
-
What you describe should work. The problem is caused by your implementation. Please post the relevant source code. – VLL Dec 05 '15 at 20:59
-
I want to have loop running and then execute a function when a key is pressed. – Hamza Khan Dec 07 '15 at 22:18
1 Answers
0
You can use something like this _kbhit.
You can also Refer this a stackoverflow answer. _kbhit returns a nonzero value if a key has been pressed. Otherwise, it returns 0. You can use this to enter your function optionally.
Please specify your operating System. This is for Windows.
For Unix like Operating System refer this.
And If you are referring to and embedded System, Then please elaborate your question and add more details.

Abhijeet Kumar Srivastava
- 131
- 12