Thread: Need help with Text Input

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    Need help with Text Input

    K here is the problem. I am just messing around developing for the Ipod using Ipod Linux. However the Ipod only has 8 keys. What this causes is that when the wheel is scrolled to the left the "l" key is pressed and when scrolled to the right the "r" key is called and when the center button is hit the "enter" key is called.

    Now I would like to be able to let the user hit all the keys. I already have found someone that has made it so that you can hit the characters but it only receives one character. If someone could help me edit this function to take a full string it would be greatly appreciated. Also anyone that could make it so that I could turn the characters into corresponding integers would help even more.

    Here is the code to get a single character.
    Code:
    // This function is used to get only one character.
    int mygetch()
    {
    	struct termios oldt, newt;
    
    	int ch;
    	tcgetattr( STDIN_FILENO, &oldt );
    	newt = oldt;
    	newt.c_lflag &= ~( ICANON | ECHO );
    	tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    	ch = getchar();
    	tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    
    	return ch;
    }
    
    char getinput(char *charlist)
    {
    	char input;
    	char flag;
    	char curfl = 0;
    
    	flag = charlist[curfl];
    	putc(flag, stdout);
    
    	while (1)
    	{
    		if (input = mygetch())
    			switch(input)
    			{
    				case 'r':	// user rotated right, advance character.
    					if (curfl < strlen(charlist)-1)
    					{
    						putc(8, stdout);
    						curfl++;
    						flag = charlist[curfl];
    						putc(flag, stdout);
    					}
    					break;
    				case 'l':	// user rotated left, reverse character.
    					if (curfl > 0)
    					{
    						putc(8, stdout);
    						curfl--;
    						flag = charlist[curfl];
    						putc(flag, stdout);
    					}
    					break;
    				case '\n':	// select button
    					return flag;
    					break;
    				default:	// any other character!
    					break;
    			}
    	}
    }
    Now heres the actual way the function is called hopefully someone could help me also take integers and hopefully more than one character:

    Code:
    char foo;
    foo = getinput("abcdefghijklmnopqrstuvwxyz");

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to modify it to take a string. Simply call it in a loop until your "end of string" denotation is entered, and return everything entered. Either use a fixed buffer size, and go until it's full, or they enter "end of string", or dynamically allocate and reallocate a string to return.

    Furthermore, it looks like all you have to do is simply add "0123456789" to the passed string, and use something like atoi on it. Or read the FAQ here on getting a string from the user, and modify its portion on reading numbers to suit.

    Oh, and is this really what you want?
    Code:
    if (input = mygetch())
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    This is as far as I can get:

    Code:
    int currchar=0;
    char input, charlist[] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=+!@#$%^&*()[]{}_-<>\"\'1234567890\\/.";
    while(1) {
       if(read(fileno(stdin),&input,1) != -1) // read 1 character of input
          switch(input) {
             case 'r': // user rotated right, advance character.
                (currchar==strlen(charlist)-1)?(currchar=0):(currchar++);
                write(fileno(stdout),&charlist[currchar],1); // write the character
                write(fileno(stdout),"\b",1); // move the cursor back over the character
                break;
             case 'l': // user rotated left, decrement character.
                (currchar==0)?(currchar=strlen(charlist)-1):(currchar--);
                write(fileno(stdout),&charlist[currchar],1);
                write(fileno(stdout),"\b",1);
                break;
             ...
          }
    }
    if someone could please help turn that into a function that does the above described stuff it would be greatly appreciated.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [edit]Never mind.[/edit]

    As for this,
    Code:
    charlist[] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX  YZ=+!@#$%^&*()[]{}_-<>\"\'1234567890\\/.";
    . . . look into isalpha() ans isprint(), <in ctype.h>.

    [edit]
    Oh yes, and don't press tab, type \t (in the string).
    [/edit]
    Last edited by dwks; 08-07-2005 at 12:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Choosing a variable based on user text input.
    By Compiling... in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2005, 01:21 AM
  2. Parsing Text File and gathering input variables
    By azamsharp1 in forum C Programming
    Replies: 2
    Last Post: 10-26-2005, 08:43 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  5. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 03:17 PM