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

2024-05-11

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

我如何在不冻结程序的情况下读取密钥,例如:

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 http://www.programmingsimplified.com/c/conio.h/kbhit

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c 编程检查是否按下按键而不停止程序 的相关文章

随机推荐