GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Sep-2004, 02:21
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

How to interpret characters as they are being entered?


Hi all,

I am sure there must be some easy way to do it, but can someone let me know the way to read and interpret the chars or numbers as they are being entered?

Like for e.g in case of CLI parser,a parser will ideally take a full command till user hits "enter" or "return" key.But what if i want parser to stop reading futher command when user hits some special characters such as "?" or "TAB" or "ESC".I want to break the character reading loop at that point and take whatever been inputted so far and pass it for special processing.(Something like Unix bash shell,when you hit "TAB" the command input stops and command completion event starts and command is completed appropriately)

Thanks,
  #2  
Old 08-Sep-2004, 04:40
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Something like this?

CPP / C++ / C Code:
	char c, breakChar = '\t';

	char command[100]; // Specify desired size
	int i = 0;
	while ( ( c = getche() ) != breakChar )
	{
		command[i++] = c;
	}
	command[i] = '\0';

	printf("\n%s", command);

Or, for some special characters, like Esc, use their code.

CPP / C++ / C Code:
	char c;
	char command[100]; // Specify desired size
	int i = 0;

	while ( (int) (c = getch()) != 27 )
	{
		printf("%c", c);
		command[i++] = c;			
	}
	command[i] = '\0';

	printf("\n%s", command);

Of course, using getche with special characters will result in the printing of funny things, so just use getch and do the eco yourself.
To find out the code of the special characters, use something like this:

CPP / C++ / C Code:
	char test = getch();
	printf("%d", test);

Hope this helps.

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 08-Sep-2004, 05:34
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Quote:
Originally Posted by LuciWiz
Something like this?

CPP / C++ / C Code:
	char c, breakChar = '\t';

	char command[100]; // Specify desired size
	int i = 0;
	while ( ( c = getche() ) != breakChar )
	{
		command[i++] = c;
	}
	command[i] = '\0';

	printf("\n%s", command);

Or, for some special characters, like Esc, use their code.

CPP / C++ / C Code:
	char c;
	char command[100]; // Specify desired size
	int i = 0;

	while ( (int) (c = getch()) != 27 )
	{
		printf("%c", c);
		command[i++] = c;			
	}
	command[i] = '\0';

	printf("\n%s", command);

Of course, using getche with special characters will result in the printing of funny things, so just use getch and do the eco yourself.
To find out the code of the special characters, use something like this:

CPP / C++ / C Code:
	char test = getch();
	printf("%d", test);

Hope this helps.

Regards,
Luci


Hi,

I am currently using following loop to collect my commands on command line
CPP / C++ / C Code:
while ((ch1=getchar())!='\n')
	{
		cmd1[i]=ch1;
		i++;
	}
	cmd1[i]='\0';

This loop exits when i hit enter after entering my command.I tried modifying the condition in while loop to check for "\t" as well(with && and ||). However,it doesn't seem to take effect unless i hit enter.

whats the difference between getch() and getchar()? I use gcc complier on linux and it does not allow me to use getch() function even when i have the necessary header file "curses.h" as per the man pages on getch().How should i go about it?

Thanks,
  #4  
Old 08-Sep-2004, 07:30
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by nkhambal
whats the difference between getch() and getchar()? I use gcc complier on linux and it does not allow me to use getch() function even when i have the necessary header file "curses.h" as per the man pages on getch().How should i go about it?

Thanks,

What do you mean by does not allow?
Try compiling with -lncurses flag, like so: cc program.c -lncurses

With getchar you need to push Enter after the character. Stick with getch for this program!

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 08-Sep-2004, 07:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
Hi,

I am currently using following loop to collect my commands on command line
CPP / C++ / C Code:
while ((ch1=getchar())!='\n')
	{
		cmd1[i]=ch1;
		i++;
	}
	cmd1[i]='\0';

This loop exits when i hit enter after entering my command.I tried modifying the condition in while loop to check for "\t" as well(with && and ||). However,it doesn't seem to take effect unless i hit enter.

whats the difference between getch() and getchar()? I use gcc complier on linux and it does not allow me to use getch() function even when i have the necessary header file "curses.h" as per the man pages on getch().How should i go about it?

Thanks,


The standard C library function getchar() buffers the input. That is, keystrokes are stored in a buffer, and are not made available to the user until "Enter" is pressed. Then they are fed to the user program, one at a time, as getchar() is called.

The "good" thing about this is that you can backspace and make corrections any time before you press "Enter", and it's taken care of without your program having to handle such things. The "bad" thing is that if you want to act on any particular key as it is typed, you can't.

Function getch() was supplied by Borland in its Turbo C compiler (somewhere around 1985, I thnk, but I forget exactly) to allow the program to see any key as it is pressed. getch() does not echo the characters to the screen, so the program will have to do that (presumably after it has acted on the special characters that you want to identify. Function getche() does the same as getch(), except it does echo each character as it is typed (the e in getche() reminds us of the echo function). Other compilers may have getch() or some equivalent function, but it's not in the C standard library.




Try the program at this link: http://www.gidforums.com/t-3386.html

You can use it with your program like this:

CPP / C++ / C Code:
#include <stdio.h>

int linux_getch(void);

int main()
{
  char cmd1[BUFSIZ];
  char ch1;

  int i;
  i = 0;

  while (((ch1=linux_getch())!='\n') && (ch1 != '\t') && (i < sizeof(cmd1)-1))
  {
    cmd1[i]=ch1;
    putchar(ch1);
    i++;
  }
  cmd1[i]='\0';
  
  printf("\n\nYou entered: <");
  for (i = 0; cmd1[i]; i++) 
  {
    putchar(cmd1[i]);
  }
  printf(">\n");

  return 0;
}

I called the function linux_getch(), but you can call it anything you like. Use the test program at the above link to see what linux_getch() actually returns as you type any "normal" or any "special" keys.


Regards,

Dave
Last edited by JdS : 08-Sep-2004 at 08:22. Reason: Please insert [thread]thread number[/thread] tags to create links to other threads here
  #6  
Old 08-Sep-2004, 08:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by LuciWiz
What do you mean by does not allow?
Try compiling with -lncurses flag, like so: cc program.c -lncurses

With getchar you need to push Enter after the character. Stick with getch for this program!

Regards,
Luci

Have you tried this with the example program that the original poster gave? (What version of Linux?) My understanding is that if you want to use curses or ncurses, you have to do lots of other things beyond simply calling linking the library and using getch() in the manner indicated.

But, I'm not an ncurses user.

Will you post a complete linux program that uses getch() like this?

Regards,

Dave
  #7  
Old 08-Sep-2004, 12:29
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Thanx dave,

The function linux_getch() did the trick for me.Just one problem as you have already mentioned that the line is now no more editable.i.e.I can not use backspace it edit the line,delete the characters that i entered.

Any idea how to get arround this problem.?Can you suggest some code or algorithm to do this?

Thanks again.

Nilesh.
  #8  
Old 08-Sep-2004, 12:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
Thanx dave,

The function linux_getch() did the trick for me.Just one problem as you have already mentioned that the line is now no more editable.i.e.I can not use backspace it edit the line,delete the characters that i entered.

Any idea how to get arround this problem.?Can you suggest some code or algorithm to do this?

Thanks again.

Nilesh.

I don't have anything ready-to-go.

You can read chars into a buffer. If the user enters a backspace, you decrease the char count and echo <backspace><space><backspace> to the screen to erase the previous character. (Of course you check to make sure it doesn't do anything if a backspace is entered when you are at the beginning.)There may be other things you want to do as well, but this is probably the minimum. Finally, when the user presses "Enter" or whatever terminating character you have indicated, you process the chars in the buffer.

If you want to let the user to use arrow keys or keys like <Home>, <End>, etc., you can use my test program to see that when a special function key is pressed several characters are returned. So, you have to detect those keys (it's not hard, once you know what to look for), then you have to do whatever the program should do for each one.



Regards,

Dave
  #9  
Old 09-Sep-2004, 01:06
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by davekw7x
Have you tried this with the example program that the original poster gave? (What version of Linux?) My understanding is that if you want to use curses or ncurses, you have to do lots of other things beyond simply calling linking the library and using getch() in the manner indicated.

No, I didn't try it. That's why I told him to try this, because I don't use Linux any more. I joined the dark forces (Win). I did use it though....and it wasn't that complicated, I guess it depends on the platform and compiler (it was some version of Red Hat and gcc). Also, I understand that there are serious problems even when switching from one version of ncurses to the other (on some machines). I only suggested this aproach because I didn't know how to "fake" getch on Linux (thanks for that, btw).


Quote:
Originally Posted by davekw7x
Will you post a complete linux program that uses getch() like this?

CPP / C++ / C Code:
#include <ncurses.h>

int main()
{	
	initscr();			/* Start curses mode 		  */
	printw("Hello World !!!");	/* Print Hello World		  */
	refresh();			/* Print it on to the real screen */
	getch();			/* Wait for user input */
	endwin();			/* End curses mode		  */

	return 0;
}

This is an example I took from a book and should work...I can't try it anymore though (because of the hall Win thing ).

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #10  
Old 09-Sep-2004, 01:16
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
This is another example from Pradeep Padala's NCURSES Programming HOWTO.

CPP / C++ / C Code:
#include <ncurses.h>

int main()
{	int ch;

	initscr();			/* Start curses mode 		*/
	raw();				/* Line buffering disabled	*/
	keypad(stdscr, TRUE);		/* We get F1, F2 etc..		*/
	noecho();			/* Don't echo() while we do getch */

    	printw("Type any character to see it in bold\n");
	ch = getch();			/* If raw() hadn't been called
					 * we have to press enter before it
					 * gets to the program 		*/
	if(ch == KEY_F(1))		/* Without keypad enabled this will */
		printw("F1 Key pressed");/*  not get to us either	*/
					/* Without noecho() some ugly escape
					 * charachters might have been printed
					 * on screen			*/
	else
	{	printw("The pressed key is ");
		attron(A_BOLD);
		printw("%c", ch);
		attroff(A_BOLD);
	}
	refresh();			/* Print it on to the real screen */
    	getch();			/* Wait for user input */
	endwin();			/* End curses mode		  */

	return 0;
}

I was just curious what exactly doesn't work? Is it a compilation error? Does the program run at all?

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Factorial of numbers cior C++ Forum 7 09-Jun-2004 20:08
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.