Sign in to follow this  

Getting a keyboard string from SDL

This topic is 4730 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

char strbuf[1024]
while(!done)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
{
int key = event.key.keysym.sym
if((key>=SDLK_a)&&(key<=SDLK_z))
{
char s[2];
s[0] = key-SDLK_a;
s[1] = 0;
strcat(strbuf,s);
}
}
break;
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
*** Source Snippet Removed ***


Retorical question: And what when the input exceeds 1023 characters? :)

Btw, it might be better to use the SDL unicode translator, then you'll automatically support uppercase/lowcase characters as well.
Check out SDL_EnableUNICODE() in your documentation...

Share this post


Link to post
Share on other sites
Quote:
Original post by LordMyth
(I've read about the unicode thing, but it doesn't change the method eh)


noo not at all, simply read out of event.key.unicode instead. The following macro will tell you whether the character is printable or not (i.e. whether you really want to add it to your string):


#define BLAHBLAH(x) (x && !(x&0xff80)) /* where x is from event.key.unicode */



Remember SDL_EnableUNICODE(TRUE); in your initialization code.

Quote:

Really no other way to solve it??


At least you should also check for strlen(strbuf)<sizeof(strbuf)-1 before appending the character. (to overcome the overflow problem)

Share this post


Link to post
Share on other sites
I haven't tested this but you should get the idea:


//vector to hold all of the typed characters
std::vector<std::string> TypedChar;

//event structure
SDL_Event Event;

while(SDL_PollEvent(&Event)
{
switch(Event.type)
{
case SDL_KEYDOWN:
{
TypedChar.push_back(SDL_GetKeyName(Event.key.keysym.sym));
break;
}
}
}



SDL_GetKeyName takes an SDLKey and returns a char* string representing the character that was pressed.


Share this post


Link to post
Share on other sites
This is the code I use right now, and the only thing is that enters are displayed as something like two spaces. But it may be my text drawing function, it only handles standard "\n" newlines (ASCII code 10). How is a newline in SDL (or unicode??)?
[EDIT:] it's a space, not two spaces
[EDIT 2:] in fact it's not even a space, wordpad interprets it as a newline, notepad does display it as a [] sorta box.

[EDIT 3]
OK!! it's solved, it was code 13


case SDL_KEYDOWN:
{
if ( event.key.keysym.unicode ) {
KeyboardString += event.key.keysym.unicode;
}
}






[Edited by - LordMyth on January 4, 2005 7:43:43 AM]

Share this post


Link to post
Share on other sites

This topic is 4730 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.

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