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.
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?
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?