keyboard input in SDL
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?
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).
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?
You have to use SDL_PollEvent and look for the Keyboard Events
keysym has information on states such as if shift,alt or ctrl is pressed and such.
Good Luck
keysym has information on states such as if shift,alt or ctrl is pressed and such.
Good Luck
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...
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement