Sign in to follow this  
Kelly G

keyboard input in SDL

Recommended Posts

I'm working on a SDL/OpenGL game that is fullscreen and "grabbed." There will be times when the player has to enter string data into the keyboard. SDL_GetKeyState() doesn't seem well-suited for this as it simply gives you a bitmap of keys pressed. Is there a favored method in sdl for getting ascii data from the keyboard? Maybe even distinguish upper- and lower-case characters?

Share this post


Link to post
Share on other sites
Every time a key is pressed you will also get an event, which you would handle in your main loop. The event structure will also indicate such things as modifier keys (shift/alt etc).

Share this post


Link to post
Share on other sites
Thank you for replying. It looks like I will have to write my own input entry system, but I think I'm beginning to see how to do this. Also is there an SDL or other platform-independant way to query whether Caps Lock is on or off?

Share this post


Link to post
Share on other sites
This is how I do it

Every time a SDL_KEYDOWN event is received I add the key to the keyboard map and a buffer

So in my processEvents rotuine...

case SDL_KEYDOWN:
this->keyboard_device.setKeyDown(event.key.keysym.sym);

The function looks like this...


void KeyboardDevice::setKeyDown(SDLKey key)
{
//key is both pressed and down
this->key_info[key] = (KEY_PRESSED | KEY_DOWN_STATE);

//Add this key to the buffer if space available
if (this->key_buffer.size() < BUFFER_SIZE)
{
this->key_buffer.push(static_cast<KeyCode>(key));
}
}



key_info is an array where key state is stored

key_buffer is obviously the buffer

Then simply use a getKey routine to pop the keys out of the buffer.

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