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

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

问题描述

我试图创建一个函数,将的printf 如果用户presses键盘上的任意按键,除了资本某些字符串 p ,如果用户presses p 那么它会打破循环。

不过,我不认为我使用 _kbhit _getch 正常。我用的是80号,因为这是80的ASCII符号....对不起任何混乱

 无效activateAlarm(INT的channelID){    INT键= 0;    而(temperatureChannel [的channelID] .currentTemperature> temperatureChannel [的channelID] .highLimit
        || temperatureChannel [的channelID] .currentTemperature< temperatureChannel [的channelID] .lowLimit){        蜂鸣声(350100);        如果(_kbhit()){
            键= _getch();
            如果(键=='P');
                打破;
        }
    }
}


解决方案

没有必要说明的是,code会谈更好的:

 的#include<&CONIO.H GT;// ...的printf(请preSS P键暂停\\ n);INT键= 0;而(1)
{
    如果(_kbhit())
    {
      键= _getch();      如果(键=='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.

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 \n ");

int key = 0;

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

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

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

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