C语言 如何使用 kbhit 和 getch(C 编程)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15603082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use kbhit and getch (C programming)
提问by Pardon_me
I'm trying to create a function that will printfa certain string if the user presses any button on the keyboard EXCEPT for capital P, if the user presses Pthen it will break the loop.
我正在尝试创建一个函数,printf如果用户按下键盘上的任何按钮P,如果用户按下键盘上的任何按钮,该函数将输入某个字符串,如果用户按下P,则会中断循环。
However I don't think I'm using _kbhitand _getchproperly. I use the number 80 because that is the ASCII symbol for 80....sorry for any confusion
但是我认为我没有正确使用_kbhit和_getch正确。我使用数字 80,因为这是 80 的 ASCII 符号......抱歉有任何混淆
void activateAlarm(int channelID) {
int key = 0;
while(temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit
||temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) {
beep(350,100);
if (_kbhit()) {
key = _getch();
if(key == 'P');
break;
}
}
}
回答by masoud
No need to explain, the code talks better :
无需解释,代码讲得更好:
#include <conio.h>
// ...
printf("please press P key to pause \n ");
int key = 0;
while(1)
{
if (_kbhit())
{
key =_getch();
if (key == 'P')
break;
}
}

