gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:59:53 25 4
gpt4 key购买 nike

我正在尝试在终端中构建一个简单的俄罗斯方块游戏。当玩家按下箭头键时,我需要移动棋子。我听说过 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 arrowkey) and wgetch(3x) returns a single value representing the functionkey, as in KEY_LEFT. If disabled (bf is FALSE), curses does not treatfunction keys specially and the program has to interpret the escape sequences itself. If the keypad in the terminal can be turned on (madeto transmit) and off (made to work locally), turning on this optioncauses the terminal keypad to be turned on when wgetch(3x) is called.The default value for keypad is FALSE.

关于c++ - ncurses:在 C++ 中处理终端中的键盘输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64866790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com