• Advertisement


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

DInput: detecting when a key is pressed

This topic is 6323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
ralph.potter@digital-magi.com, http://www.digital-magi.com

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

Share this post

Link to post
Share on other sites
oh, i see.

how would i go about getting buffered input to record key presses then?

Share this post

Link to post
Share on other sites

  • Advertisement