Finding the difference between holding the key and pressing it repeatedly

Started by
0 comments, last by EGD Eric 20 years ago
So far, Win32 has served my purpose just fine for making this game, but now I''m not sure how to figure out the difference between holding a key and pressing it rapidly. What I''ve been doing is just filling a 256 bool array in WinProc, then checking the array for whether a key is down or not. Now, I want the player to be able to shoot rapidly by pressing the key real fast, and shoot at a steady speed by just holding it. I''m not sure how to do that with Win32. Does DirectInput have easy ways of doing this?
Advertisement
Every time the key is pressed, it will generate a WM_KEYDOWN message.

When you hold down a key, it will generate a WM_KEYDOWN message when you first press it down, then after a brief delay (determined by the key repeat delay you have set in Windows), it will start generating those messages rapidly at a constant interval (determined by the key repeat interval set in Windows).

So...
For someone pressing the key very fast, each time they do it, it will make a WM_KEYDOWN message, so it should be easy enough to just put a Shoot() command in there when you handle that message. However, you also have to take into consideration the fact that if someone is holding down the key, it will eventually start generating a lot of these messages. If you''re content to live with the Windows settings for the interval and delay, then you have no problem - just add the Shoot() command, and everything will work.

If instead you want to write your own method for dealing with shooting when the key is held down, first you have to make sure that the Shoot() command is only called when the key is _NOT_ being held down. Something like this:
bool KeyState[256];//...switch (msg){//...case WM_KEYDOWN:if (wParam == SHOOT_BUTTON){if (!KeyState[wParam])Shoot();}KeyBuf[wParam]=true;break;case WM_KEYUP:KeyBuf[wParam]=false;break;//...}


Then, within your main game loop, you create an if-statement that calls the shoot function every x seconds if KeyState[SHOOT_BUTTON] is true.

Hope that helps.

P.S. There isn''t really anything (AFAIK) that you should ever need DirectInput for if you''re only dealing with mouse and keyboard input.

This topic is closed to new replies.

Advertisement