Key repeat?

Started by
3 comments, last by YodaTheCoder 22 years, 6 months ago
Curious as to how I may end this bloody (that''s with some British emphasism) key thing.. You know when you push a character, it inputs it, waits a little, then starts an input loop? Well, I''ve been trying to get rid of that while I use the ''a'' and ''d'' key''s to move my figure around the screen. Any functions? ~Jesse "Sleep is to me as honest is to lawyer."
Advertisement
That''s what DirectInput is for, largely. You can also look into the WM_KEYDOWN window message.
Here''s how I do it without DirectInput. Yes, DI is better to use but I haven''t learned it yet.

//////////////////////////////////////////////////// Global Variables//////////////////////////////////////////////////BOOL keyPressed[256];BOOL keyDown[256];...LRESULT CALLBACK WndProc(...){...case WM_KEYDOWN:  keyPressed[wParam] = TRUE;  return 0;  break;case WM_KEYUP:  keyPressed[wParam] = FALSE;  return 0;  break;...} 


And wherever you want to test your keys:

// for a non repeating keyif (keyPressed[''A''] && !keyDown[''A'']){  keyDown[''A''] = TRUE;  // do stuff here (will not repeat until key is let go of)}else if (!keyPressed[''A''] && keyDown[''A''])  keyDown[''A''] = FALSE;// for a repeating keyif (keyPressed[''D'']){  // do stuff here (will repeat while key is down)} 


Yes, this may not be the best way but it works very well for me without having to learn DI. =) Hope this is of some help and if anyone sees and problems with the way I do it, let me know.

- Mike
another way to do it (again without DirectInput) is to use the GetAsyncKeyState(...) function.

This will tell you whether a key is down or up at the moment you call it... so just stick this call in your game loop, or in a timer function, or wherever you feel like checking your keys... i don''t care
Just use DirectInput. The SDK''s help files tell you everything you need to know, and once you''ve got round the pain of setting up objects etc it''s very easy to use =]

This topic is closed to new replies.

Advertisement