win32 input problem

Started by
1 comment, last by lollan 15 years, 9 months ago
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
Advertisement
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.

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.

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 ;}

This topic is closed to new replies.

Advertisement