首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mac中的kbhit()

mac中的kbhit()
EN

Stack Overflow用户
提问于 2008-11-23 05:57:54
回答 2查看 6.6K关注 0票数 1

我是mac的cpp新手。当我在我的程序中使用kbhit()时,我得到了错误。我使用了#include,但也得到了错误,所以我使用#include进行了搜索和测试,但错误仍然存在。所以请帮帮我。提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2008-11-23 06:10:43

不知道这是否能在Mac上工作,但这里有一些我用来在Linux上获得单次按键的代码。

代码语言:javascript
复制
int mygetch() {
    char ch;
    int error;
    static struct termios Otty, Ntty;

    fflush(stdout);
    tcgetattr(0, &Otty);
    Ntty = Otty;

    Ntty.c_iflag  =  0;     /* input mode       */
    Ntty.c_oflag  =  0;     /* output mode      */
    Ntty.c_lflag &= ~ICANON;    /* line settings    */

#if 1
    /* disable echoing the char as it is typed */
    Ntty.c_lflag &= ~ECHO;  /* disable echo     */
#else
    /* enable echoing the char as it is typed */
    Ntty.c_lflag |=  ECHO;  /* enable echo      */
#endif

    Ntty.c_cc[VMIN]  = CMIN;    /* minimum chars to wait for */
    Ntty.c_cc[VTIME] = CTIME;   /* minimum wait time    */

#if 1
    /*
    * use this to flush the input buffer before blocking for new input
    */
    #define FLAG TCSAFLUSH
#else
    /*
    * use this to return a char from the current input buffer, or block if
    * no input is waiting.
    */
    #define FLAG TCSANOW

#endif

    if ((error = tcsetattr(0, FLAG, &Ntty)) == 0) {
        error  = read(0, &ch, 1 );        /* get char from stdin */
        error += tcsetattr(0, FLAG, &Otty);   /* restore old settings */
    }

    return (error == 1 ? (int) ch : -1 );
}
票数 2
EN

Stack Overflow用户

发布于 2008-11-23 06:02:46

kbhit()是非标准的。事实上,我不相信有一个标准的函数来检测键盘输入。你能做的最好的事情就是使用例如fgetc从stdin中读取一个字符,并且希望它不会被从其他地方重定向。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/312185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档