Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL keyboard

Started by BloodLust666 Dec 4, 2005 at 5:03 PM 1 replies 1.6k views
Original Post
BloodLust666
BloodLust666
I'm just now getting into SDL and i've started with the input stuff. I was looking at the help page and whatnot and i've made something that looks like this.
[source lang=c++]
	class cInput  
	{
	private:
		SDL_Event		m_Event;

	public:
		cWInput();
		~cWInput()	{Shutdown();};
	    
		bool Init();
		void Shutdown();

		void Read();

		bool GetKeyPressed(long Key);
		bool GetKeyReleased(long Key);
		bool GetKeyState(long Key);
	};
	void cWInput::Read()
	{
		SDL_PollEvent( &m_Event );
	}

	bool cWInput::GetKeyPressed(long Key)
	{
		if ( m_Event.key.type == SDL_KEYDOWN && (m_Event.key.keysym.sym & Key))
			return true;

		return false;
	}
	
	bool cWInput::GetKeyReleased(long Key)
	{
		if ( m_Event.key.type == SDL_KEYUP && (m_Event.key.keysym.sym & Key))
			return true;

		return false;
	}
	
	bool cWInput::GetKeyState(long Key)
	{
		if ( m_Event.key.keysym.sym & Key )
			return true;

		return false;
	}

am i looking at this correctly? by what i'm getting from the help page. i'm thinking that the KEY_PRESSED AND KEY_RELEASED event happen RIGHT when the key is pressed or released. Then if the key is still being pressed, it is taken out of the KEY_PRESSED event and put into the KEY_DOWN even. Example: Pressing Key_H = SDL_KEYPRESSED event triggered while Key_H is still pressed = SDL_KEYPRESSED event = false AND SDL_KEYDOWN = true for Key_H Also: How would i test to see if multiply keys are being pressed? would this be a way? if (m_Event.key.keysym.sym & SDLK_h) // H is pressed if (m_Event.key.keysym.sym & SDLK_i) // I is pressed
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Simian Man
Simian Man
I don't think that will be very useful. Only if the last event was the key press will you get correct results. (For example, if I press 'A' and hold it, your system will only show it as pressed until another event occurs (like moving the mouse).) If you want this kind of architecture where at any time you can query any key, you should look intoSDL_GetKeyState

Good Luck
BloodLust666
BloodLust666
oh, so, don't keep "SDL_PollEvent()"'ing it. do SDL_PumpEvents() then every frame call "SDL_GetKeyState(NULL)"... correct?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.