🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

DInput.

Started by
4 comments, last by Alexander Deruwe 24 years, 6 months ago
Surely if the code that responds to a key-press is being executed three or four times per key-press this would be because your main loop is very fast (say >60fps) and it's impossible to actually press a key for 1/60th of a second. DirectInput is very fast.
All sorts of other things can cause what look like input problems. For instance, I was using BltFast in windowed mode and because I wasn't properly waiting for my Blits to finish, they were being queued which lead to the input looking like it had a 2 second delay.
Advertisement
Using a delay and repeat system works very nicely if you have the time to implement it. However, if you want to move left once for every physical keypress (with no repeating) you could just utilize buffered-input from DInput to check for a keypress event, or you could compare the last recorded state with the current state to the same effect.

- Splat

Unless u actually need a realtime key state(like in flight simulators), the delay algorithms suck. They're always gonna be either too fast, or too slow, or have the strange feel.
The best way to deal with the problem is to implement buffered input, it works perfectly, and is relatively easy to set up. There is an article on GDNet somewhere about DirectX buffered input, look for it, even though it's for directx 3, it explains how to do it very well.
You should definitely look at buffered device input as a solution to your problem, as the previous two posts say. I would just add that it is useful (and rather simple) to just set up an class (if you're using C++) that just enumeratates all of message buffers of the input devices that you need buffered data for.

This way you can get messages from all of the devices in sequential order (using the macro DISEQUENCE_COMPARE(dwSequence1, cmp, dwSequence2)).

Of course if you want to be able to have a key repeat a down message during a long keypress you would need to add some additional code to your game loop.

Just my two pence.

------------------
Where's my Golden Fleece?

This question from a few weeks ago popped up while I was writing my last reply:

I have my main game loop, and in it I get the state of all keys with DI. Now, when I check them, it goes way too fast.
Sometimes when you hit "left" once, you go left 4-5 positions, just because the keypress is too slow (I think).

I've tried slowing this down with some timer code, but that isn't what it is supposed to be, since now there is like a delay sortof-feel on the keys.

Does anyone know what I might do to correct this problem?

/Alexander Deruwe
The best way that I have found to do it (in my opinion and if memory isn't to big of a deal) is to create another array of bools that you update to show that the key was pressed, not just held down. My code looks like this:

m_ButtonsClk[0] = (!m_Buttons[0] && (mouseState.rgbButtons[0]&0x80));
m_ButtonsClk[1] = (!m_Buttons[1] && (mouseState.rgbButtons[1]&0x80));
m_ButtonsClk[2] = (!m_Buttons[2] && (mouseState.rgbButtons[2]&0x80));
m_ButtonsClk[3] = (!m_Buttons[3] && (mouseState.rgbButtons[3]&0x80));
m_ButtonsClk[4] = (!m_Buttons[4] && (mouseState.rgbButtons[4]&0x80));
m_ButtonsClk[5] = (!m_Buttons[5] && (mouseState.rgbButtons[5]&0x80));
m_ButtonsClk[6] = (!m_Buttons[6] && (mouseState.rgbButtons[6]&0x80));

m_Buttons[0] = (mouseState.rgbButtons[0] & 0x80 );
m_Buttons[1] = (mouseState.rgbButtons[1] & 0x80 );
m_Buttons[2] = (mouseState.rgbButtons[2] & 0x80 );
m_Buttons[3] = (mouseState.rgbButtons[3] & 0x80 );
m_Buttons[4] = (mouseState.rgbButtons[4] & 0x80 );
m_Buttons[5] = (mouseState.rgbButtons[5] & 0x80 );
m_Buttons[6] = (mouseState.rgbButtons[6] & 0x80 );

where mouseState is the updated DIMOUSESTATE, m_Buttons tells whether the key is down(pressed or being held down), and m_ButtonsClk tells whether or not the button was pressed (and not being held down). This seems to work well for everything that I have done. Hope this helps.

---Ranok---

This topic is closed to new replies.

Advertisement