Skip to main content
gamedev.net
🔒 Locked

Direct Input problems

Started by MartyJ Jul 10, 2006 at 8:13 AM 3 replies 850+ views
Original Post
MartyJ
MartyJ
I'm having trouble with direct input. I am using direct input in immediate mode and am wanting to use to control game state changes, eg pause. The key to pause and unpause is the same and the problem i'm having is that the key is registered more than once in a single key press. I'm using the GetDeviceState() function every frame so I understand why this is happening. In order try fix this I simply held the last state of the each key. When A key is queried I only return true if the the key is pressed and was not pressed in the previous frame. This doesn't seem to be working however. This is the class i'm using to represent a single key.

class CActionState
{
public:
	CActionState() : _state(false), _lastState(false) {}
	void SetState(bool newState) 
	{
		_lastState = _state;
		_state = newState; 
	}
	bool GetState() const {return _state;}
	bool GetNewPress() const {return _state && !_lastState;}

private:
	bool _state;
	bool _lastState;
};

GetNewPress() is the function i'm wanting to use for state changes. This however doesn't work and i'm not sure how. Can anyone see anything wrong with my code? Or could it be how I set up direct input?
adam23
adam23
I've had the same problem you are having. Here is what I did to solve it. Save for each key the last frame the key was pressed. That way you can ignore if the key was pressed within the last couple frames.

unsigned long m_keyPressStamp[256]; // Stamps the last frame each key was preseed.

Each of the Keys has an integer value so DIK_PAUSE would be like this.
m_keyPressStamp[DIK_PAUSE] = currentFrame;

What I did is create a function that takes the key pressed and a flag whether to ignore the stamp.

bool GetKeyPress(int key, bool ignoreStamp){if( ( m_keyState[key] & 0x80 ) == false )return false;if(!ignoreStamp){if(m_keyPressStamp[key] > frame - 2)return false;}//Additional code here this is only an abbreviate function
Adamhttp://www.allgamedevelopment.com
Supernat02
Supernat02
Is everything synchronized? If you have two SetState calls prior to your GetNewPress call, this would be causing you problems. Maybe put a counter in both functions and make sure they are synchronized for a quick test.
Chris ByersMicrosoft DirectX MVP - 2005
MartyJ
MartyJ
Supernat02: SetState() is called once every frame after the GetDeviceState() call, which in my current program is about 1500 - 2000fps since its doing very little.

adam23: This solution did occur to me and I think I may go with it. I feel that if my current solution worked it would be simpler and just as accurate than using time stamps. I'm pretty sure the logic is sound so its really confusing me why it isn't working.

Am I correct in thinking that while a key is pressed GetDeviceState() should show that the key is pressed every time it is queried until the key is released?
arithma
arithma
Quote:
Original post by MartyJ
Am I correct in thinking that while a key is pressed GetDeviceState() should show that the key is pressed every time it is queried until the key is released?

Yes.

I think the whole problem could be solved more elegantly if you use directinput bufferred data events thingies.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.