A quick experiment:
#include <stdio.h>
#include <curses.h>
int main(void) {
printf("KEY_UP = %d\n", (int)KEY_UP);
}
indicates that 259
is the value of KEY_UP
, the value that getch()
normally returns when it reads an up-arrow (since there's no single ASCII or even Unicode code for that key).
At least in xterm, there are two distinct sets of code sequences for the arrow keys. The up arrow key normally sends the sequence ^[[A
, where ^[
is the Escape key. But when I enable "Application Cursor Keys" in xterm's middle-click menu, the up arrow key instead sends ^[OA
, which is consistent with the 27 OA
you're seeing.
So if getch()
sees the ^[[A
sequence, it recognizes it as an up-arrow key and returns KEY_UP
. But it doesn't recognize ^[OA
, so it just returns it as a sequence of 3 distinct characters. (The sequence that getch()
recognizes is controlled by the terminfo entry for your terminal type, depending on the value of $TERM
.)
Find out how to disable "Application Cursor Keys" in your terminal emulator; that should fix the problem.