![]() |
|
#1
|
|||
|
|||
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
|
||||
|
||||
|
Something like this?
CPP / C++ / C Code:
Or, for some special characters, like Esc, use their code. CPP / C++ / C Code:
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:
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
|
|||
|
|||
|
Quote:
Hi, I am currently using following loop to collect my commands on command line CPP / C++ / C Code:
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
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
Quote:
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:
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
Quote:
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 Quote:
CPP / C++ / C Code:
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
|
||||
|
||||
|
This is another example from Pradeep Padala's NCURSES Programming HOWTO.
CPP / C++ / C Code:
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 GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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