无法读取键盘输入

问题描述 投票:0回答:1

[尝试获取用户输入的简单终端游戏。我在Mac OS上。

#include <stdio.h>
#include <curses.h>
#include <iostream>

int main()
{
    int ch;
    while (ch != 113)
    {
        ch = getch();
        std::cout << ch << std::endl;
    }

    return 0;
}

在此示例中,我试图简单地打印击键,但是ch = getch()似乎没有任何作用。它不等待按键,并且std::cout << ch << std::endl仅重复打印-1。无法弄清楚我在做什么错。

c++ c curses getch keyboard-input
1个回答
0
投票

[您需要先调用initscr,然后再执行其他curses功能。http://www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html

#include <stdio.h>
#include <curses.h>
#include <iostream>

int main()
{
    int ch;
    initscr(); // <----------- this
    while (ch != 113)
    {
        ch = getch();
        std::cout << ch << std::endl;
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.