如你所知,在Windows中使用getch()时,应用程序会等到你按下一个键,
如何在不冻结程序的情况下读取密钥,例如:
void main(){
char c;
while(1){
printf("hello\n");
if (c=getch()) {
.
.
.
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
您可以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;
}
Run Code Online (Sandbox Code Playgroud)
更多信息:http://www.programmingsimplified.com/c/conio.h/kbhit