How to use kbhit and getch (C programming) [closed]

谁都会走 提交于 2019-12-17 09:59:50

问题


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.

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

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:


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


来源:https://stackoverflow.com/questions/15603082/how-to-use-kbhit-and-getch-c-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!