I'm writing a programm that's using getch()
to scan for arrow keys. My code so far is:
switch(getch()) {
case 65: // key up
break;
case 66: // key down
break;
case 67: // key right
break;
case 68: // key left
break;
}
Problem is that when I press 'A'
, 'B'
, 'C'
or 'D'
the code will also executed, because 65
is the decimal code for 'A'
, etc...
Is there a way to check for an arrow key without call others?
Thanks!
Actually, to read arrow keys one need to read its scan code. Following are the scan code generated by arrow keys press (not key release)
Byte 50 is 80 DOWN
In the above code I have assumed programmer wants to move 4 lines only.
I'm Just a starter, but i'v created a
char(for example "b")
, and I dob = _getch();
(its aconio.h
library's command) And checkAnd do check for the keys (72 up, 80 down, 77 right, 75 left)
So, after alot of struggle, I miraculously solved this everannoying issue ! I was trying to mimic a linux terminal and got stuck at the part where it keeps a command history which can be accessed by pressing up or down arrow keys. I found ncurses lib to be painfuly hard to comprehend and slow to learn.
It's kind of bold but it explains alot. Good luck !
I have written a function using getch to get arrow code. it's a quick'n'dirty solution but the function will return an ASCII code depending on arrow key : UP : -10 DOWN : -11 RIGHT : -12 LEFT : -13
Moreover,with this function, you will be able to differenciate the ESCAPE touch and the arrow keys. But you have to press ESC 2 time to activate the ESC key.
here the code :