Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在按下c中的按钮之前一直打印_C - Fatal编程技术网

如何在按下c中的按钮之前一直打印

如何在按下c中的按钮之前一直打印,c,C,我是c编程新手 我正在编写一个代码,该代码要求程序一直运行,直到我按下一个特定的键(不是用enter键,而是用一个按钮) 比如说- 我的代码一直在打印“Hello World” 只要我按下按钮,说出字母a,程序就会停止 我尝试使用getch() 但它会暂停代码,直到我碰到一个字符,然后转到下一行。 我如何克服这个问题。 有人能帮忙吗。试着从winuser.h使用GetKeyState()。这里有一个[链接][1]帮助您理解 虽然你不应该在控制台中处理按键 编辑: “GetKeyState()”仅

我是c编程新手 我正在编写一个代码,该代码要求程序一直运行,直到我按下一个特定的键(不是用enter键,而是用一个按钮)

比如说-

我的代码一直在打印“Hello World”

只要我按下按钮,说出字母a,程序就会停止

我尝试使用
getch()
但它会暂停代码,直到我碰到一个字符,然后转到下一行。
我如何克服这个问题。

有人能帮忙吗。

试着从
winuser.h
使用
GetKeyState()
。这里有一个[链接][1]帮助您理解

虽然你不应该在控制台中处理按键

编辑:

“GetKeyState()”仅适用于WIN32 API

使用getch。 下面的演示-

#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include <clib.h>//sorry for the long list of headers(I was originally using this code for some big project)
char q;
int main(int argc, char *argv[]){
    printf("enter value-");
    while(1){
if(kbhit()){

q=getch();
if(int(q)==115){  //I'm using 's' for the keypress

    printf("\nSuccess");

}
else{

    printf("\nYou didn't press s");

}
}



    }

















while(!kbhit()); //pauses console
return 0;

}```




#包括
#包括
#包括
#包括
#包括
#包括
#包括
#include//很抱歉标题列表太长(我最初在某个大型项目中使用此代码)
charq;
int main(int argc,char*argv[]){
printf(“输入值-”);
而(1){
if(kbhit()){
q=getch();
如果(int(q)=115{//我正在使用's'作为按键
printf(“\n成功”);
}
否则{
printf(“\n您没有按s”);
}
}
}
同时(!kbhit());//暂停控制台
返回0;
}```

阅读关于图书馆的信息我试过了。但是由于我使用的是Ubuntu,我的gcc找不到conio.h,kbhit()。这就是为什么我尝试使用curses.h和getch()但无法找到解决方案的原因。你能帮我用咒语吗?
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include <clib.h>//sorry for the long list of headers(I was originally using this code for some big project)
char q;
int main(int argc, char *argv[]){
    printf("enter value-");
    while(1){
if(kbhit()){

q=getch();
if(int(q)==115){  //I'm using 's' for the keypress

    printf("\nSuccess");

}
else{

    printf("\nYou didn't press s");

}
}



    }

















while(!kbhit()); //pauses console
return 0;

}```