I am trying to move a cursor using arrow keys but there is a delay of one key with these. I read that it sends the escape sequence first so this could be why, is there any way to fix it?
int main() {
int ch,x=0,y=0;
initscr();
noecho();
cbreak();
keypad(stdscr, TRUE);
refresh();
while(true) { /*main loop*/
ch=getch();
switch (ch) {
case KEY_UP:
y--;
move(y,x);
break;
case KEY_DOWN:
move(y,x);
y++;
break;
case KEY_RIGHT:
move(y,x);
x++;
break;
case KEY_LEFT:
move(y,x);
x--;
break;
case 27:
goto clean;
default:
addch(ch);
break;
}
}
clean:
endwin();
return 0;
}
Thanks!
As noted in a comment, the problem with the example program is that in some cases it adjusts the coordinate after moving the cursor, inconsistent with the case for KEY_UP
, which adjusts the coordinate before moving the cursor.
To be consistent, put the move(y,x)
calls after the y++
, x++
and x--
statements.