Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ NCurses getch始终返回ERR(-1)_C++_Linux_Ncurses_Ros_Stringstream - Fatal编程技术网

C++ NCurses getch始终返回ERR(-1)

C++ NCurses getch始终返回ERR(-1),c++,linux,ncurses,ros,stringstream,C++,Linux,Ncurses,Ros,Stringstream,我刚刚开始使用ROS,我正在尝试编写一个节点来发布主题中的键 我使用ncurses在Linux Ubuntu 16.04.4上创建了一个节点 这是我的代码: #include <curses.h> #include "ros/ros.h" #include "std_msgs/String.h" #include <sstream> int main(int argc, char **argv) { int ch; nodelay(stdscr, T

我刚刚开始使用ROS,我正在尝试编写一个节点来发布主题中的键

我使用ncurses在Linux Ubuntu 16.04.4上创建了一个节点

这是我的代码:

#include <curses.h>

#include "ros/ros.h"
#include "std_msgs/String.h"

#include <sstream>

int main(int argc, char **argv)
{
    int ch;
    nodelay(stdscr, TRUE);

    ros::init(argc, argv, "keyboard_driver");

    ros::NodeHandle n;

    ros::Publisher key_pub = n.advertise<std_msgs::String>("keys", 1);

    ros::Rate loop_rate(100);

    while (ros::ok())
    {
        std_msgs::String msg;
        std::stringstream ss;

        if ((ch = getch()) != ERR)
        {
            ss << ch;
            std::cout << ch;
            msg.data = ss.str();

            ROS_INFO("%s", msg.data.c_str());

            key_pub.publish(msg);
        }

        ros::spinOnce();
        loop_rate.sleep();
    }

    return 0;
}
调试它时,我发现getch()总是返回-1

我该怎么做才能让它工作

更新

我尝试过这个小程序,但它不打印任何内容:

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

int main(int argc, char **argv)
{
    int ch;
    cbreak();
    nodelay(stdscr, TRUE);

    for(;;)
    {
        if ((ch = getch()) != ERR)
        {
            std::cout << ch;
        }
    }

    return 0;
}
#包括
#包括
int main(int argc,字符**argv)
{
int-ch;
cbreak();
节点延迟(stdscr,真);
对于(;;)
{
如果((ch=getch())!=ERR)
{

std::cout您已经设置了
nodelay
,因此
getch
将在终端没有准备好数据时立即返回
ERR
。这就是getch返回-1(ERR)的原因.您没有将cbreak或raw设置为禁用终端缓冲,因此您仍然可以得到这样的结果--在按下Enter键之前,不会有数据从终端出来


因此,在开始时添加对
cbreak()
的调用(就在对
nodelay()
的调用之前或之后),它应该可以像您预期的那样工作。

您已经设置了
nodelay
所以
getch
如果终端没有准备好数据,就会立即返回
ERR
。这就是getch返回-1(ERR)的原因.您没有将cbreak或raw设置为禁用终端缓冲,因此您仍然可以得到这样的结果--在按下Enter键之前,不会有数据从终端出来

因此,在开始时添加对
cbreak()
的调用(就在对
nodelay()
的调用之前或之后),它应该按照您的预期工作。

要使用
getch()
,您必须执行以下操作:

#include <iostream>
#include <curses.h>
#include <signal.h>
#include <stdlib.h>

void quit(int sig)
{
    endwin();
    exit(0);
}

int main(int argc, char **argv)
{
    int ch;

    signal(SIGINT,quit);

    initscr();
    cbreak();
    nodelay(stdscr, TRUE);

    for(;;)
    {
        if ((ch = getch()) != ERR)
        {
            std::cout << ch;
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
无效退出(int sig)
{
endwin();
出口(0);
}
int main(int argc,字符**argv)
{
int-ch;
信号(SIGINT,退出);
initscr();
cbreak();
节点延迟(stdscr,真);
对于(;;)
{
如果((ch=getch())!=ERR)
{
std::cout要使用
getch()
您必须执行以下操作:

#include <iostream>
#include <curses.h>
#include <signal.h>
#include <stdlib.h>

void quit(int sig)
{
    endwin();
    exit(0);
}

int main(int argc, char **argv)
{
    int ch;

    signal(SIGINT,quit);

    initscr();
    cbreak();
    nodelay(stdscr, TRUE);

    for(;;)
    {
        if ((ch = getch()) != ERR)
        {
            std::cout << ch;
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
无效退出(int sig)
{
endwin();
出口(0);
}
int main(int argc,字符**argv)
{
int-ch;
信号(SIGINT,退出);
initscr();
cbreak();
节点延迟(stdscr,真);
对于(;;)
{
如果((ch=getch())!=ERR)
{

std::cot谢谢你的回答,但我已经尝试过了,但它不起作用。我用一个小程序更新了我的问题,以测试
getch()
,它不会输出任何语句
std::难道我忘了添加
initscr()谢谢你的回答,但我已经试过了,但没有用。我用一个小程序更新了我的问题,测试
getch()
,它没有输出任何语句
std::我是否忘了添加
initscr();