Jump to content
  • Advertisement
Sign in to follow this  
LGAB

SDL for text typing

This topic is 3628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm writing a 2D GUI, using opengl to draw my widgets and SDL for various things such as windowing, keyboard and mouse. Currently I'm writing a textbox widget to allow typed text input from the user, but I'm realizing that SDL's way of handling the keyboard isn't ideal for typing, as it just returns a list of key states, so if the user types fast, not letting the previous key up before pressing the next I'll get multiple letters at once, and in alphabetical order instead of the order in which they were pressed. I used Glut before for another similar project, and its keyboard functions seems more suited for this, I think it has a queue of keyboard events which it pipes to the current event handler function one at a time, and it also gives you actual characters such as an @-character instead of just states for shift and 2. So is there something I can do to make SDL behave the same way, or do I have to convert my app to Glut to make something like this? I mean, even games need text input sometimes, couldn't be the first time this was done. :) I appreciate any thoughts on the subject, thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:

Currently I'm writing a textbox widget to allow typed text input from the user, but I'm realizing that SDL's way of handling the keyboard isn't ideal for typing, as it just returns a list of key states, so if the user types fast, not letting the previous key up before pressing the next I'll get multiple letters at once, and in alphabetical order instead of the order in which they were pressed.

I used Glut before for another similar project, and its keyboard functions seems more suited for this, I think it has a queue of keyboard events which it pipes to the current event handler function one at a time, and it also gives you actual characters such as an @-character instead of just states for shift and 2.

So is there something I can do to make SDL behave the same way, or do I have to convert my app to Glut to make something like this? I mean, even games need text input sometimes, couldn't be the first time this was done. :)
SDL has an event polling system, which is probably what you'll want to use here. Look into SDL_Event, SDL_PollEvent(), etc.

Share this post


Link to post
Share on other sites
Thanks for pointing me in the right direction! I was already using pollevent to capture the keydown event, but I was using GetKeyState instead of reading the unicode character from the event, which makes more sense of course :)

With the changes below I can type like expected in the cmd window (for quick testing), I just have to pipe that information to the right keyboard handler now.


SDL_EnableKeyRepeat(300, 50);
SDL_EnableUNICODE(1);

[...]

case SDL_KEYDOWN:
if (event.key.keysym.sym>27) cout << char(event.key.keysym.unicode);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!