tags:

views:

83

answers:

1

On Unix, when I press up arrow key, it shows this string, but while scanf, it does not take it as input. Please explain how to take it as input. Can we something like compare the character by charater like first ^[ is Esc key and so on?

+6  A: 

That's the escape sequence generated by that key. '^[' is CTRL-[ (the ESC character), and the other two characters are '[' and 'A'.

If you want to process them, you'll need to read all three characters and decide that they mean the user pressed the up-arrow key.

Whether or not you can do this with your scanf depends on the format string. I would be using a lower level of character input for this.

I never use [f]scanf in real code since failure results in you not knowing where the input pointer is located. For line-based input, I find it's always better to use fgets and then sscanf the string retrieved.

But, as I said, you should be using getc and its brethren for low-level character I/O. Or find a higher level function such as readline under Linux, or other libraries that know to convert it into special keycodes such as VK_KEY_UP that you can process.

paxdiablo
Okay I got it.I have one problem. Like in shell,as soon as when we press the arrow key, it shows the command history instantaneouly. But the above comaprison method requires to take new line terminated string and then compare character by charac.Please tell me how does the shell processes the above 3 character combination as soonas the charater is pressed?
avd
It sets the terminal to RAW mode. In this mode, the terminal sends you the characters as they are typed. In COOKED mode (the default for apps), the terminal collects a whole line and then sends that to the app.
Aaron Digulla
More than likely, bash (or readline which I think it uses under the covers) has put the terminal into raw mode where characters are given immediately to the application. The alternative is cooked mode where only a complete line is deliverd to the app. This is from the early days when it was inefficient to have the computer process charcaters. The mainframe was even worse in that it sent whole screens up to the program when you pressed ENTER.
paxdiablo
If you want to allow command line editing in your app, check put the readline library.
Aaron Digulla