I am trying to build a simple tetris game within the terminal. I need to move the pieces when the player presses an arrow key. I heard about the getch() method but it doesn't seem to work with arrow keys (they were all detected with the same int code: 27). What should I do?
Here is my current code:
initscr();
int inputCode;
do
{
inputCode = getch();
// here I would put my code to move the pieces if the code is right
} while (inputCode != 113); // q to exit
endwin();
Sincerely, Thomas
The as noted in the manual page:
Most programs would additionally use the sequence:
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
That is, your program should turn on keypad mode to get KEY_UP
, etc:
The keypad option enables the keypad of the user's terminal. If enabled (bf is TRUE), the user can press a function key (such as an arrow key) and wgetch(3x) returns a single value representing the function key, as in KEY_LEFT. If disabled (bf is FALSE), curses does not treat function keys specially and the program has to interpret the escape sequences itself. If the keypad in the terminal can be turned on (made to transmit) and off (made to work locally), turning on this option causes the terminal keypad to be turned on when wgetch(3x) is called. The default value for keypad is FALSE.