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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s the escape sequence generated by that key.
'^['isCTRL-[(theESCcharacter), 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
scanfdepends on the format string. I would be using a lower level of character input for this.I never use
[f]scanfin 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 usefgetsand thensscanfthe string retrieved.But, as I said, you should be using
getcand its brethren for low-level character I/O. Or find a higher level function such asreadlineunder Linux, or other libraries that know to convert it into special keycodes such asVK_KEY_UPthat you can process.