GetAsyncKeyState uses all CPU

Hello!

I'm a beginner in C++ programming and I want to program a simulator for a little car on a racetrack. I have some problems with the keyboard input.

I use GetAsyncKeyState to read the keyboard input. This works fine except that it freezes when I hold down a button for a long time. This is because GetAsyncKeyState uses all the CPU...

I also tried with ReadConsoleInput but it only takes one keystroke at a time, so i can't press W and A att the same time, so I can't drive forward and make a left turn at the same time...

How can I solve this problem? I'm sure it's not too hard.

Thanks in advance!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  if (cMode == c_manual)
		{
			wPress = GetAsyncKeyState(87) & 0x8000;
			aPress = GetAsyncKeyState(65) & 0x8000;
			sPress = GetAsyncKeyState(83) & 0x8000;
			dPress = GetAsyncKeyState(68) & 0x8000;

			throttleActive = false;
			steerActive = false;

			if (aPress && !dPress) // Left turn
			{
				if (us < 1.0f)
					us += 0.1f;
				steerActive = true;
			}
			if (dPress && !aPress) // Right turn
			{
				if (us > -1.0f)
					us += -0.1f;
				steerActive = true;
			}
			if (wPress && !sPress) // Gas
			{
				if (ug < 1.0f)
					ug += 0.1f;
				throttleActive = true;
			}
			if (sPress && !wPress) // Brake
			{
				if (ug > -1.0f)
					ug += -0.1f;
				throttleActive = true;
			}
			if (!steerActive)
				us = 0;
			if (!throttleActive)
				ug = 0;

			Sleep(10);
		}   //Close if(mMode) 
This is because GetAsyncKeyState uses all the CPU...


Nope. GetAsyncKeyState is nonblocking so it returns immediately regardless of whether or not the key is being pressed.

You are misdiagnosing the problem. The deadlock must be caused by something else in your code.


Also... on a side note... why the magic numbers? Why not just use the character? Much easier and much more clear:

1
2
3
4
wPress = GetAsyncKeyState('W') & 0x8000; // <- 'W' instead of 87
aPress = GetAsyncKeyState('A') & 0x8000; // <- 'A' instead of 65
sPress = GetAsyncKeyState('S') & 0x8000; // <- 'S' instead of 83
dPress = GetAsyncKeyState('D') & 0x8000; // <- 'D' instead of 68 
Thanks for your answer.

I still think this is the problem in my code.

The code I posted is inside of a while-loop that runs all the time. The problem is e.g. when I hold down W a long time (2 seconds), the program freezes. I read somewhere that this is becouse GetAsyncKeyState() returns values all the time, putting it into some buffer, and blocking my drawing program from drawing.

Do you think this would be solved if I made the drawing program run with higher priority than the input-part of the program?

And I didn't know it could be done as simply as GetAsyncKeyState('W'), makes the code easier to understand, thanks for that!
The code I posted is inside of a while-loop that runs all the time.


?? Are you processing incoming messages? How are you drawing if you never leave that loop?

I read somewhere that this is becouse GetAsyncKeyState() returns values all the time, putting it into some buffer, and blocking my drawing program from drawing.


GetAsyncKeyState returns immediately. As soon as you call it, it returns and gives you the key state. It does not block your program from doing anything.

This problem has nothing to do with GetAsyncKeyState. Put this function out of your mind. The problem must be elsewhere in your program.

Do you think this would be solved if I made the drawing program run with higher priority than the input-part of the program?


Increasing priority is kind of ambiguous unless you are multithreading (which you really, really shouldn't be). And no, I don't think that would solve your problem.

If your program is deadlocking it must be because you have a logic deadlock somewhere. IE, you're getting stuck in an infinite loop.




It's hard to comment without seeing more of the program. One thing I can say for sure is that this problem has absolutely nothing to do with GetAsyncKeyState.

It would help if you uploaded the full source somewhere so I can take a look. Can you zip it up and put it on DropBox or something?
Topic archived. No new replies allowed.