Sign in to follow this  
AntonyCoder

How to improve this keyboard handler?(Code incl)

Recommended Posts

Hey, Can you see any way to improve this keyboard handler for my TextField class? Technically it works, but in practise it's sluggish and really ackward to use. It's been years since I've needed to write one so I'm really out of touch. Would you suggest using threads to improve stability? If it makes a difference, I'm using SDL events KEY_UP/KEY_DOWN to update a 1024 bool wide array called keys. So if keys[ asciicode ] == true, then keydown.
	if( _isactive == true )
		{
			if( !(_deltick == NULL ))
			{
				if( _deltick->Click() )
				{
					delete _deltick;
					_deltick = NULL;
				}
			}
			if( keys[ SDLK_BACKSPACE ] )
			{
				if( _deltick == NULL )
				{
					DelChar();
					_deltick = new Timer( 250 );
				}
			}
			if( !(_spctick == NULL ))
			{
				if( _spctick->Click() )
				{
					delete _spctick;
					_spctick = NULL;
				}
			}
			if( keys[ SDLK_SPACE ] )
			{
				if( _spctick == NULL )
				{
					SpaceChar();
					_spctick = new Timer( 250 );
				}
			}

		
			if( _keyin == false )
			{
				_keytick->Invalidate();
			}
			_keyin = false;
			int j = KeyDown();
			if( !(j == -1 ) )
			{
				if( !(j == _lkey ))
				{
					_keyin = false;
				}
				else
				{

					if( _res == false )
					{
					_keytick->Reset();
						_res = true;
					}

					_keyin = true;
				}
				if( _keytick->Click() )
				{
					SetChar( _actc,(char)j );
					_lkey = j;
					_res = false;
				}
			}
					

						
			
		}

SetChar/DelChar/SpaceChar just edit the text buffer, no timing checks are done in those funcs. Here is the KeyDown function that returns the active key.
int KeyDown()
{
	char *k = "abcdefghijklmnopqrstuvwxyz012345678890.,\n";
	char *tf = (char *)malloc(2);
	tf[1] = 0;
	for(int i=0;i<1024;i++)
	{
		if( keys[i] )
		{
			tf[0] = i;
			int ret=StringUtil->Find( k,tf,0 );
			if(!(ret == -1 ))
			{	
				return i;
			}
		}
	}
	return -1;
	free( (void *)tf );
}

Share this post


Link to post
Share on other sites

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