how to use _getch_nolock() ??

Hi, I'm trying to create a loop that keeps going until the user presses a key, I'm currently using _getch() which pauses the thread until there's an input, which I don't want. I've tried using _getch_nolock() but the same thing still happens, here's the script I'm trying to use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>
#define ever ;;

using namespace std;

void main()
{
	cout << "Press ENTER to Start!";
	for(ever)
	{
		int key = _getch_nolock();
		if(key == 13)
			break;

		cout<<endl<<"endl";
	};
}
IMHO you should not continuously poll for keyboard input, because it would use 100 percent of the CPU for nothing. If tell us more about what problem you are trying to solve, we might come up with a better solution.

But if you really want to do what you say, the solution will probably be platform-specific, so we need to know which platform you are working on.
im trying to detect when there's an input to move a character (in this case a dash) across the screen when a certain key is pressed, im trying to do this to create a simple pong style game. im working on windows xp with visual c++ 2008.
That there function is a Windows monstrosity...

On Windows, you can check to see if a key is pressed with:

(with delay) http://www.cplusplus.com/forum/beginner/5619/#msg25047
(sans delay) http://www.cplusplus.com/forum/general/3389/page1.html#msg14326

Using a delay is better because it allows the system to handle your thread much more nicely... and has no noticable effect against your program.

Make sure to read the input once you know that a key is pressed.

Hope this helps.


[edit] Wait, you're using using VS2008? How exactly are you doing this: as a GUI application or a Console application? If you are using a GUI application, you should be listening for key events in your main form. If it is a console application, you should be familiar with the Windows Console Functions http://www.google.com/search?btnI=1&q=msdn+Console+Functions [/edit]
Last edited on
i just started as an empty project, i guess its a console application. i dont really understand the functions you linked me to, the one that seems to look like a good idea is ReadConsoleInput but i have no idea how to use it, also i would like my application to do something while waiting for an input, i guess the easiest way to do this is with multithreading, but im still not sure how to that, the only thing i know is in windows you use _beginthread but is there a better way than this e.g. writing my own function to do it, or a better library. any examples would be brilliant.
Topic archived. No new replies allowed.