DirectX getting keyPressed

Started by
14 comments, last by simpler 13 years, 4 months ago
Don't get much about std::map

Anyways, you think this code will do for storing 2 keyboardstates?

poll(){	// Poll keyboard.	HRESULT hr = mKeyboard->GetDeviceState(sizeof(mLastKeyboardState), (void**)&mKeyboardState);         HRESULT hr = mKeyboard->GetDeviceState(sizeof(mKeyboardState), (void**)&mKeyboardState); 	if( FAILED(hr) )	{		// Keyboard lost, zero out keyboard data structure.		ZeroMemory(mKeyboardState, sizeof(mKeyboardState));		 // Try to acquire for next time we poll.		hr = mKeyboard->Acquire();	}


And then in Menu::keyPressed(char key)
I check if mLastKeyboardState[key] is false and mKeyboardState[key] is true.

I'm not sure how the poll() function is suppose to look like, dunno if that I posted is valid. Think it is?
Advertisement
What you want is more like:
poll(){	// Store old state	memcpy(mLastKeyboardState, mKeyboardState, sizeof(mKeyboardState));	// Poll keyboard.        HRESULT hr = mKeyboard->GetDeviceState(sizeof(mKeyboardState), (void**)&mKeyboardState); ...
You don't want to poll the keyboard twice, just store the state from the previous frame.
Thanks alot Evil Steve!
Just one last question. When i pull the function the very first time, what value will be copied to mLastKeyboardState then? Ain't that going to be undefined?
If you want to, just do a poll() once in your initialization code, before any objects call poll(), to set mKeyboardState. Yes, when the first time memcpy is executed, it will copy garbage-to-garbage but the next call to poll() will save the initial state.

However, it's likely your program will execute poll() several times before the user has a chance to press a key and all will be well in any case.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Okey I will try to implement this and see how it goes.
A big <3 to Evil Steve and all who helped me. I got it to work just like I wanted with this code:

void DirectInput::poll(){	// Poll keyboard.	memcpy(mLastKeyboardState, mKeyboardState, sizeof(mKeyboardState)); 	HRESULT hr = mKeyboard-&gt;GetDeviceState(sizeof(mKeyboardState),       (void**)&mKeyboardState); ...}bool DirectInput::keyPressed(char key){		return (!mLastKeyboardState[key] && mKeyboardState[key] & 0x80 ) != 0;	}


Thanks alot![grin]

This topic is closed to new replies.

Advertisement