c 编程检查是否按下按键而不停止程序

标签 c windows

如您所知,在 Windows 中使用 getch() 时,应用程序会等待您按下某个键,

如何在不卡住程序的情况下读取 key ,例如:

void main(){
  char   c;
  while(1){
  printf("hello\n");
  if (c=getch()) {
  .
  .
  .
  }  
}

谢谢。

最佳答案

您可以使用kbhit()来检查是否按下了某个键:

#include <stdio.h>
#include <conio.h> /* getch() and kbhit() */

int
main()
{
    char c;

    for(;;){
        printf("hello\n");
        if(kbhit()){
            c = getch();
            printf("%c\n", c);
        }
    }
    return 0;
}

更多信息请点击:http://www.programmingsimplified.com/c/conio.h/kbhit

关于c 编程检查是否按下按键而不停止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55606641/

相关文章:

c - C中定义数组类型问题

windows - 如果我知道 Windows 内核,我能做什么?

python - 从 SVG 文件创建开罗路径

windows - 有没有办法让 Chocolatey 显示下载进度?

c - kbhit() 作为转义触发器

c - 功能的可用性?

C - 对数组描述进行排序

c++ - 如何使用 ODBC 处理长字符串?

c - 使用异或在数组中重复和缺失的数字

windows - Windows 中 Ruby 的 GUI 编辑器