21

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?

Saurav Pathak
  • 796
  • 11
  • 32

3 Answers3

23

I found the same problem on Mac OS X. But it was resolved by adding the following:

keypad(stdscr, TRUE);
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Paul Franz
  • 239
  • 2
  • 2
19

I was storing getch() calls as char's when they were supposed to be int's. Works perfectly after the switch.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • Actually, this should have been closed because the question did not show the program that would have demonstrated the problem. – Thomas Dickey Feb 19 '23 at 14:55
9

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
Ryan Ballantyne
  • 4,064
  • 3
  • 26
  • 27