This topic is now archived and is closed to further replies.


DInput: detecting when a key is pressed

Recommended Posts

Quantum    122
i know how to detect if a key is down using directinput, but i''m having trouble detecting when its pressed i want to be able to call a function that will tell me if it has been pressed this frame.. so i can hold it down and it wont repeat im doing this, but it doesn''t seem to be working

//.. Get the state of the keyboard and store it in Keys ..

//Set the LastKey to on, if it was previously off and is on this frame
//Set it to off if it was previously on and it is off this frame

for(int i = 0; i < sizeof(Keys); i++)
	if((Keys<i> & 0x80) && (!(LastKey[i] & 0x80)))
		LastKey[i] = 1;

	else if((!(Keys[i] & 0x80)) && (LastKey[i] & 0x80))
		LastKey[i] = 0;			
its probably something stupid.. but i want to get this working can somebody help?

Share this post

Link to post
Share on other sites
digital-magi    122
I think that would work if it didn't get called so often. Basically waht I suspect happens is that you press a key and the next time your loop runs it tags it as pressed. The second time around the chances are you probably still holding the key down so

Keys<i> & 0x80

will be true again. This causes the input to toggle between true and false for as long as you hold the key down.

I suggest that you take a look at buffered input. It should solve all your problems since that records key presses rather than key states.

Ralph Potter,

Edited by - digital-magi on October 30, 2000 8:34:50 AM

Share this post

Link to post
Share on other sites