Advertisement

Getting a keyboard string from SDL

Started by January 02, 2005 01:31 PM
8 comments, last by LordMyth 19 years, 8 months ago
How can you get an array of typed characters with SDL?
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;		}	}}
Advertisement
Can you explain a bit how it all works?
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...
-- Rasmus Neckelmann
Biiig trouble will happen!
(I've read about the unicode thing, but it doesn't change the method eh)
Really no other way to solve it??
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)
-- Rasmus Neckelmann
Advertisement
Ehh, sorry but it's all really confusing to me, can redo the first example with unicode and explain step-by-step?
I haven't tested this but you should get the idea:

//vector to hold all of the typed charactersstd::vector<std::string> TypedChar;//event structureSDL_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.


BTW, isn't it better to just add the char to the string using the '+' operator?
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]

This topic is closed to new replies.

Advertisement