Page 1 of 1

need help with keyboard input (sdl)

Posted: Mon Jun 21, 2010 6:19 pm
by cndr
I am having a problem following lazy foos tutorial, I decided to use char instead of a string, the problem that I am encountering is that when I use event.key.keysym.unicode it does not update the array of chars, and the problem with event.key.keysym.sym is that it does not recognize capital letters. The only reason why I do not want to use string is because certain functions I want to use require chars, any one have any ideas on how to get unicode to work?

Code: Select all

//counter = 0;
//char writing[40]

			if (event.type == SDL_KEYDOWN)
			{
			SDL_EnableUNICODE( SDL_ENABLE ); 
				if (counter < 40)
				{
		                       if( ( event.key.keysym.sym == (Uint16)' ' )  || 
                                 	( event.key.keysym.sym >= (Uint16)'0' ) && ( event.key.keysym.sym <= (Uint16)'9' ) ||
                                 	( event.key.keysym.sym >= (Uint16)'A' ) && ( event.key.keysym.sym <= (Uint16)'Z' ) ||
                                 	( event.key.keysym.sym >= (Uint16)'a' ) && ( event.key.keysym.sym <= (Uint16)'z' ) ) 
					{ 
					writing[counter] = (char)event.key.keysym.sym ; 
					printf("sd\n");
					counter++;
					} 
				}
				printf("char is %c\n",(char)event.key.keysym.unicode );
			SDL_EnableUNICODE( SDL_DISABLE ); 
			}

Re: need help with keyboard input (sdl)

Posted: Mon Jun 21, 2010 6:49 pm
by Live-Dimension
A char can't hold Unicode characters, obviously. A char is ascii, and only a byte, where as unicode has characters from 1-byte up to 4-bytes per character. As for capital letters, check for shift/caps lock and make letter capital as accordingly.

If you want to deal with Unicode, really, your program needs to be Unicode based. your looking for the "wide char", which is wchar_t. That is the Unicode variant of char. wstring is the Unicode variant of string.

Re: need help with keyboard input (sdl)

Posted: Mon Jun 21, 2010 8:41 pm
by cndr
thanks for the help Live-Dimension, I didn't think about the fact that chars are not able to hold unicode.

Re: need help with keyboard input (sdl)

Posted: Tue Jun 22, 2010 7:17 pm
by Live-Dimension
I forgot, you also need specialized functions to deal with unicode strings/charaters. They generally are the same name but end/start with a u *i think*. Check that out.

Re: need help with keyboard input (sdl)

Posted: Thu Jun 24, 2010 11:39 am
by cndr
I didn't realize before that I could us var.c_str() to use strings in functions like when you open a file to read out of it, I ended up switching to string. Thanks for the help.

I have another question though, say I have the following code:

Code: Select all

			if (event.type == SDL_KEYDOWN && keydown == false)
			{
			keydown = true;
			}
			if (event.type == SDL_KEYUP)
			{
			keydown = false;
			}
as you can see I am attempting to make it so my code doesn't loop through when a key is pressed down, the problem I am having is that if I write too fast it doesn't recognize that the key is up. Any ideas on what I am doing wrong?

Re: need help with keyboard input (sdl)

Posted: Thu Jun 24, 2010 12:04 pm
by Ginto8
cndr wrote:I didn't realize before that I could us var.c_str() to use strings in functions like when you open a file to read out of it, I ended up switching to string. Thanks for the help.

I have another question though, say I have the following code:

Code: Select all

			if (event.type == SDL_KEYDOWN && keydown == false)
			{
			keydown = true;
			}
			if (event.type == SDL_KEYUP)
			{
			keydown = false;
			}
as you can see I am attempting to make it so my code doesn't loop through when a key is pressed down, the problem I am having is that if I write too fast it doesn't recognize that the key is up. Any ideas on what I am doing wrong?
yes. you should handle each keydown event individually to account for key repeat, and there isn't a major reason to worry about keyup events for textual input.