c++ get keyboard event

本文介绍了一个简单的C++控制台程序,该程序在Windows环境下运行,通过获取用户输入来决定是否退出程序,并展示了如何使用system函数清空屏幕及getch函数实现无回显字符读取。
#include <string>
#include <iostream>
#include "windows.h"
#include <conio.h>
#include <time.h>
#include "stdio.h"
using namespace std;

int main()
{
    char old;
    while(1){
        char c = getch();
        if (c == 'q'){
            char cc;
            do{
                system("cls");
                printf("are you sure to quit?(y/n)");
                cc = getch();
                printf("++%c",cc);
            }
            while(cc != 'y' && cc !='n');
            printf("q cc=%c\n", cc);
            if(cc == 'y'){
                break;
            }
            else{
                printf("q_old = %c\n", old);
            }
        }
        system("cls");
        printf("%c\n", c);
        old = c;
        printf("old=%c\n",old);
    }
    return 0;
}

 http://blog.youkuaiyun.com/zz457887107/article/details/6612034

转载于:https://www.cnblogs.com/fish7/p/7667024.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值