C编程检查是否关键pressed不停止程序 [英] c programming check if key pressed without stopping program

查看:130
本文介绍了C编程检查是否关键pressed不停止程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如你所知,在Windows中使用的getch()时,应用程序等待你,直到你preSS的关键,

as you know, when using getch() in windows, the applications waits for you until you press a key,

我怎么能读一个键的不结冰的程序,例如:

how can i read a key without freezing the program , example :

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

感谢您。

推荐答案

您可以使用的kbhit()来检查一个关键是pressed:

You can use kbhit() to check if a key is pressed:

#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;
}

此处了解详情:<一href=\"http://www.programmingsimplified.com/c/conio.h/kbhit\">http://www.programmingsimplified.com/c/conio.h/kbhit

这篇关于C编程检查是否关键pressed不停止程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆