【问题标题】:ncurses: handle keyboard inputs in the terminal in C++ncurses:在 C++ 中处理终端中的键盘输入
【发布时间】:2020-11-16 22:42:28
【问题描述】:

我正在尝试在终端中构建一个简单的俄罗斯方块游戏。当玩家按下箭头键时,我需要移动棋子。我听说过 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();

真诚地, 托马斯

【问题讨论】:

标签: c++ ncurses


【解决方案1】:

如manual page中所述:

   Most programs would additionally use the sequence:

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

也就是说,你的程序应该开启keypad模式来获取KEY_UP等:

键盘选项启用用户终端的键盘。如果启用(bf 为 TRUE),用户可以按下功能键(例如箭头 key) 和 wgetch(3x) 返回表示函数的单个值 键,如 KEY_LEFT。如果禁用(bf 为 FALSE),curses 不会处理 特别是功能键,程序必须自己解释转义序列。如果终端中的键盘可以打开(制作 传输)和关闭(在本地工作),打开此选项 调用 wgetch(3x) 时会打开终端键盘。 键盘的默认值为 FALSE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多