开发者

Asynchronous keyboard input on win32

I'm creating a simple 3D game on Windows 7 in C++ using the free version of the Havok physics engine. I want to use the WASD keys to move the character. The structure of the code is such that I need to capture this input asychronously; there is a function called in every frame of the scene to update the character's position (I want to try checking if a key is currently pressed instead of using some kind of listener for events). I searched around for a good solution, as I know little to nothing about win32 functions, and put this together:

if (GetAsyncKeyState(0x41) & 0x8000) posX=-1.0f; //A
if (GetAsyncKeyState(0x44) & 0x8000) posX=1.0f;  //D
if (GetAsyncKeyState(0x57) &am开发者_JAVA百科p; 0x8000) posX=1.0f;  //W
if (GetAsyncKeyState(0x53) & 0x8000) posX=-1.0f; //S

After checking with some printf statements, the visual debugger doesn't seem to be picking up any input with this. I know of WM_KEYDOWN and WM_KEYUP, but I can't find simple explanations on how to use them, and as far as I can tell they are more event-based than asynchronous.

Is there a problem with the snippet above, or should I try another approach?


Best guess: You're checking for "A" instead of "a". Unless of course you hold down the shift key as well, just pressing the a-key won't trigger your code.


It appears that my problem wasn't GetAsyncKeyState() after all, but my use of FindWindow() and GetWindowRect(). It wasn't recognizing that the current window was the visual debugger. Fixed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜