如何使用 kbhit 和 getch(C 编程) [英] How to use kbhit and getch (C programming)

查看:25
本文介绍了如何使用 kbhit 和 getch(C 编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,如果用户按下键盘上的任何按钮,除了大写的 P,如果用户按下 printfcode>P 然后它会打破循环.

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.

但是我认为我没有正确使用 _kbhit_getch.我使用数字 80,因为这是 80 的 ASCII 符号......对不起,任何混淆

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

推荐答案

不用解释,代码更会说话:

No need to explain, the code talks better :

#include <conio.h>

// ...

printf("please press P key to pause 
 ");

int key = 0;

while(1)
{
    if (_kbhit())
    {
      key =_getch();

      if (key == 'P')
        break;
    }    
}

这篇关于如何使用 kbhit 和 getch(C 编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆