[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;
}
SDL keyboard
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.
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
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
Good Luck
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement