【问题标题】:problem with a kbhit implementationkbhit 实现的问题
【发布时间】:2010-10-20 01:51:14
【问题描述】:

这是我找到的 kbhit 实现,但由于某种原因,它只是等待按下一个键,而不是返回 0 以外的其他结果。它并没有真正起到 kbhit 的作用...

int kbhit(void)
{
    struct timeval tv;
    fd_set read_fd;

    tv.tv_sec=0;
    tv.tv_usec=0;
    FD_ZERO(&read_fd);
    FD_SET(0,&read_fd);

    if(select(1, &read_fd, NULL, NULL, &tv) == -1)
        return 0;

    if(FD_ISSET(0,&read_fd))
        return 1;

    return 0;
}

谁能解释一下问题出在哪里? 顺便说一句,我正在使用 Linux。

我想你可能误解了我,并认为它实际上在击键后返回非零值。 我的问题是 kbhit 总是等待击键。

【问题讨论】:

  • 你为什么不使用我发给你的链接来回答你之前的问题的 kbhit 实现?

标签: c linux


【解决方案1】:

stdin 可能是行缓冲的。在尝试检测按键时,您需要将其切换为无缓冲。

【讨论】:

  • 从技术上讲,stdin 将处于规范模式。 setvbuf() 用于输出流。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多