Sign in to follow this  
lollan

win32 input problem

Recommended Posts

Hi, I am writing a small demo, I've got a First Person Camera for the moment that I use to look around,I took it from a Book (introduction to DirectX by Frank Luna). The thing is that this class use DirectInput and to be honest if I had a US keyboard I would have not notice that DirectInput is not good enough to input. But I did, and this is my problem. So I created a class to handle input:
class CInputHandler
{
public:
	CInputHandler(void);
public:
	virtual ~CInputHandler(void);
	virtual void inputDetected(SEvent& in) ;

	virtual bool isKeyDown(char c ) ;
	virtual bool isKeyDown(Keycode key) ;

private:
	SEvent mState ;

};

extern CInputHandler* gInputHandler ;

The inputDetected() function works fine, I get to know which key is pressed. I use it in my message handler as fallow:
	case WM_KEYDOWN:
			{
				e.type = EventType::KEYBOARD_INPUT ;
				e.keyInput.code = (Keycode)wParam ;
				e.keyInput.keyPressed = true ;

				WORD KeyAsc=0;
				GetKeyboardState(keyboard);
				ToAscii(wParam,lParam,keyboard,&KeyAsc,0);

				e.keyInput.ascii = KeyAsc ;
				gInputHandler->inputDetected(e) ;
				return 0;
			}

		
		case WM_KEYUP:
			{
				e.type = EventType::KEYBOARD_INPUT ;
				e.keyInput.code = (Keycode)wParam ;
				e.keyInput.keyPressed = false ;

				WORD KeyAsc=0;
				GetKeyboardState(keyboard);
				ToAscii(wParam,lParam,keyboard,&KeyAsc,0);

				e.keyInput.ascii = KeyAsc ;
				gInputHandler->inputDetected(e) ;
				return 0;
			}



Then I use the keyDown() method to see what's going on:
bool CInputHandler::isKeyDown(char c )
{
	if ( (mState.type == EventType::KEYBOARD_INPUT) && 
		mState.keyInput.keyPressed && (c == mState.keyInput.ascii) )
	{
		mState.keyInput.ascii = 0 ;
			return true ;
	}
	return false ;
}

bool CInputHandler::isKeyDown(Keycode key )
{
	if ( (mState.type == EventType::KEYBOARD_INPUT) && 
		mState.keyInput.keyPressed && (key == mState.keyInput.code) )
	{
			mState.keyInput.code = (Keycode)0x00 ;
			return true ;
	}

	return false ;
}

In my framework I have two method, onFrame(float dt) and onRender(). These two are executed when no Window Message has to be processed, the Camera is updated in the method onFrame(). How should I do for the Camera to be updated to move smoothly? Should I create a kind of event system which notify every classes requiring input?But If I do that do I still need my onFrame() method? I know that my questions are a bit shabby so feel free to ask me precisions I just don't know now how to be clearer, for now. [depressed] Thanks

Share this post


Link to post
Share on other sites
It appears that you set the keydown flag to 0 in your isKeyDown function.

A better way may be to just return true or false in the isKeyDown function and use WM_KEYUP to clear the keydown flag.

Share this post


Link to post
Share on other sites
Hi

Thanks buckeye ! It works fine, here my modification:



bool CInputHandler::isKeyDown(char c )
{
bool r = false ;

if ( (mState.type == EventType::KEYBOARD_INPUT) &&
mState.keyInput.keyPressed && (c == mState.keyInput.ascii) )
r = true ;

if ( ! mState.keyInput.keyPressed )
r = false ;

return r ;
}

bool CInputHandler::isKeyDown(Keycode key )
{
bool r = false ;

if ( (mState.type == EventType::KEYBOARD_INPUT) &&
mState.keyInput.keyPressed && (key == mState.keyInput.code) )
r = true ;

if ( ! mState.keyInput.keyPressed )
r = false ;

return r ;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this