How do I detect arrow keys pressed using curses in C?
22,346
Solution 1
I found the same problem on Mac OS X. But it was resolved by adding the following:
keypad(stdscr, TRUE);
Solution 2
Standard (VT100-like) terminals send a sequence of characters when the arrow keys are pressed. You just have to keep track of whether or not these are pressed in sequence. Here are the char's to watch for:
Down Arrow 0x1B 0x5B 0x42
Left Arrow 0x1B 0x5B 0x44
Right Arrow 0x1B 0x5B 0x43
Up Arrow 0x1B 0x5B 0x41
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
In trying to get input from the arrow keys via curses (ncurses) it won't catch as KEY_UP etc. I used the keypad function with a true argument but getch still returned an escaped sequence. How do I shift through the values returned by getch() and grab the arrow keys specifically?
-
Sean A.O. Harney almost 15 yearsAre there escape codes you could enter as a key combination or sequence using ctrl for these?
-
Ryan Ballantyne almost 15 yearsNo need; you can type them directly. 0x1B = esc, 0x5B = [, and 0x41 - 0x44 = A - D. Type esc[D in your terminal, and the cursor moves to the left.
-
Andrew Y almost 15 yearsThis approach pretty much fully defeats the whole purpose of working with curses library in the first place :-)
-
bpisano almost 3 yearsThis and storing
getch
asint
worked for me.