如何使用kbhit和getch(C编程)

时间:2021-07-03 16:14:54

I'm trying to create a function that will printf a certain string if the user presses any button on the keyboard EXCEPT for capital P, if the user presses P then it will break the loop.

我正在尝试创建一个函数,如果用户按下键盘上的任何按钮,将打印某个字符串除了大写字母P,如果用户按下P然后它将打破循环。

However I don't think I'm using _kbhit and _getch properly. 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;
        }    
    }
}

1 个解决方案

#1


13  

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

#1


13  

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