Archived

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

Sabonis

Checking if a key was pressed...

Recommended Posts

i am using DX 8.0 (DINPUT) and i''m trying to make it so if you push a certain key, it would change the camera view. I have a camera class set up, and i used:
 BOOL CZenKeyboard::IsKeyDown( int Key )
{

	// Check if the key was set

	if( m_KeyBuffer[Key] & 0x80 )
		return TRUE;
	else
		return FALSE;
	
}
After that, I use a boolean to check if the second view is activated or not. If it isn''t, it gets activated and if it is, it gets deactivated. My code is working fine, BUT it does everything millions of times when the key is pushed. Is there any suggestions on how to check the key once, and only once while it is being pressed? I hope the idea doesnt confuse you

Share this post


Link to post
Share on other sites
Two ways to accomplish what you want:

1. Save the previous keyboard state array before you call GetDeviceState(), and only perform action if the key has changed from UP in the previous state to DOWN in the current state.

2. Use buffered input for keyboard. Set the buffer size to a value greater than 0, then use GetDeviceData() to monitor the key event you are interested in.

Share this post


Link to post
Share on other sites
Have a static bool variable that you update to true when the first statement occurs. If it already is true, return FALSE. Now, on the second statement, set it to false. I think that will work.

Share this post


Link to post
Share on other sites
But another way, would be to slow down the key pressing. Use timeGetTime() to do this, as this:

DWORD oldt = timeGetTime();
if (timeGetTime() - oldt > 300)
{
do stuff here
oldt = timeGetTime(); //update the old time variable
}

Share this post


Link to post
Share on other sites