ncurses:在 C++ 中处理终端中的键盘输入

ncurses: handle keyboard inputs in the terminal in C++

我正在尝试在终端内构建一个简单的俄罗斯方块游戏。当玩家按下箭头键时,我需要移动棋子。我听说过 getch() 方法,但它似乎不适用于箭头键(它们都是用相同的 int 代码检测到的:27)。我该怎么办?

这是我当前的代码:

    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();

真诚的, 托马斯

manual page中所述:

   Most programs would additionally use the sequence:

       intrflush(stdscr, FALSE);
       keypad(stdscr, TRUE);

也就是说,你的程序应该开启keypad模式才能得到KEY_UP,等等:

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.