need help with keyboard input (sdl)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
cndr
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Sat Apr 24, 2010 7:03 am
Programming Language of Choice: etc.

need help with keyboard input (sdl)

Post 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 ); 
			}
Long Live The Queen!
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: need help with keyboard input (sdl)

Post 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.
Image
User avatar
cndr
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Sat Apr 24, 2010 7:03 am
Programming Language of Choice: etc.

Re: need help with keyboard input (sdl)

Post by cndr »

thanks for the help Live-Dimension, I didn't think about the fact that chars are not able to hold unicode.
Long Live The Queen!
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: need help with keyboard input (sdl)

Post 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.
Image
User avatar
cndr
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Sat Apr 24, 2010 7:03 am
Programming Language of Choice: etc.

Re: need help with keyboard input (sdl)

Post 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?
Long Live The Queen!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: need help with keyboard input (sdl)

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply