Advertisement

problems restricting user input with SDL?

Started by January 16, 2005 05:24 PM
8 comments, last by hplus0603 20 years, 1 month ago
hi, im working on an online 2d RPG, and im tweaking the raw user input. the input im talking about here is when you type in your login name and password, etc. to do this, it looks something like this:

  switch( event.type )
  {
		case SDL_KEYDOWN:
		{
			//get the key state
			skey =  event.key.keysym.sym;

			if ((event.key.keysym.unicode & 0xFF80) == 0)  //if its a unicode message
			{

			ch = event.key.keysym.unicode & 0x7F;     //grab the character
			}
		}
  }   





now, the problem is, here tabs, enters, and other strange characters will all work, and fill my string with these values. i want to restrict it so only numbers and letters will work. so, i tried doing this:

			if ((event.key.keysym.unicode & 0xFF80) == 0)  //if its a unicode message
			{
				//if this char is a letter or a number
				if((event.key.keysym.unicode >= 48 && event.key.keysym.unicode <= 57 && event.key.keysym.unicode >=65 && event.key.keysym.unicode <=90 && event.key.keysym.unicode >=97 && event.key.keysym.unicode <=122) )
				{
					ch = event.key.keysym.unicode & 0x7F;     //grab the character
				}
			}





i *think* this is what im suppoed to do. i stepped through the code and the event.key.keysym.unicode value is indeed the value im thinking it is (it is mapped to an ASCII value). however, doing this method, i can type any key and none of it is accepted... now, i also tried doing this:

if ((event.key.keysym.unicode & 0xFF80) == 0)  //if its a unicode message
{

	ch = event.key.keysym.unicode & 0x7F;     //grab the character
	
	//if this char isnt a letter or a number, make it 0
	if(!(ch >= 48 && ch <= 57 && ch >=65 && ch <=90 && ch >=97 && ch <=122) )
	{
            ch = 0;
	}
}





but, this does the same thing. no input is accepted. does anyone know what im doing wrong? thanks for any help.
FTA, my 2D futuristic action MMORPG
Ok your problem is the conditional expression:

if(!(ch >= 48 && ch <= 57 && ch >=65 && ch <=90 && ch >=97 && ch <=122) )


If ch == 65 (A), let us evaluate this

ch >= 48 && ch <= 57 && ch >=65 && ch <=90 && ch >=97 && ch <=122 1         &&     0  &&  1       && 1        && 0     &&    1 = 0


Since you ! that result, it is true. That is why it does not work. You will need to use parenthesis and a different method.

if( !((ch >= 48 && ch <= 57) || (ch >=65 && ch <=90) || (ch >=97 && ch <=122) ) )


I think that should work. But I have not tested it. At least you can see why yours does not currently work.

- Drew
Advertisement
the <cctype> header has a few functions for charactor testing that *might* be useful. see here.

I'm not sure if it's unicode or SDL friendly, but it's what came to my mind. I mention it because your char testing is an "inneficient construct" according to Stroustrup's "The C++ Programming Language", where he then recommends these functions (above).
hey guys,

wow, i feel stupid. i cant believe i didnt realize the logic i was trying to do there. obviously i should have done it the way Drew was suggesting.

im using that isalnum() function instead of doing it myself though, and it seems to be working.

thanks.
FTA, my 2D futuristic action MMORPG
btw,

slightly OT, but. in the SDL docs, it says EnableUNICODE() has some slight over-head. should i be enabling it before taking input, and disabling it after i poll for input (each frame)? or, should i just enable it one time in the begining of the program? im not sure which is faster.
FTA, my 2D futuristic action MMORPG
Quote:
Original post by graveyard filla
im using that isalnum() function instead of doing it myself though, and it seems to be working.


Great decision! I personally use those functions as well - for they get it done quick and easy.

Quote:
Original post by graveyard filla
btw,

slightly OT, but. in the SDL docs, it says EnableUNICODE() has some slight over-head. should i be enabling it before taking input, and disabling it after i poll for input (each frame)? or, should i just enable it one time in the begining of the program? im not sure which is faster.


You should just have to call it once to enable UNICODE and be done with. IMHO, do not worry about the overhead that was mentioned. I trust in the skills of the creator of SDL [wink]. But there is no point in enabling it, then disabling it every frame - which would be slower, since it is only an input state. The SDL docs for this function do not say much, but it does say, "Note that only key press events will be translated, not release events."

- Drew
Advertisement
... and for the number of keypress events you typically have in a game, it's not an issue at all.
ironically isalnum() bit me in the ass when someone wanted to create a new account and couldnt put the @ symbol in my text box [grin].
FTA, my 2D futuristic action MMORPG
When i first got into programming i started with perl. There are some really tricky things you can do to hose other people's computers with poorly written perl programs installed on them. One thing is to get the other computer to execute code that was *supposed* to be a name, password, email address, etc. typed into a textbox. Since then, i've been kinda strict about what goes into a textbox and what doesn't. I'v been perfectly happy with JustAlphaOrYouDie() as character options.

of course, i *hope* you've already figured out you could do:

if ( is_alpha(c) || c == '@' )
Quote:
if ((event.key.keysym.unicode & 0xFF80) == 0)  //if its a unicode message


That, of course, tests that it's NOT Unicode. If it's Unicode, the bits 0xff80 are SET, so the test should be != 0. Just FYI.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement