zoukankan      html  css  js  c++  java
  • C++检测键盘输入

    C/C++检测键盘输入,可以用kbhit()函数和或getch()函数。

    kbhit()的用法

    头文件包括“conio.h”。

    程序执行到kbhit()时,等待输入,但是不会停止而是继续运行,有输入时kbhit()才就返回一个非零值,否则返回0。下面是一个例子。

    #include <iostream>
    #include "conio.h"
    using std::cout;
    
    int main()
    {
        int times = 5;
        cout<<"Press any key:
    ";
        while(times)
        {
            if(!kbhit())
            {
                cout<<"No input now!
    ";
            }
            times--;
        }
    
        return 0;
    }

    输出:

    程序执行到 if(!kbhit()) 时,因为当前没有输入,所以会连续打印“Now input now!”五次结束。中间的时间根本来不及打断。

    如果写个下面的例子,更加直观。

    #include <iostream>
    #include "conio.h"
    using std::cout;
    
    int main()
    {
        while(!kbhit())
        {
            cout<<"Now input now!
    ";
        }
    
        return 0;
    }

    程序会不断地打印“Now input now!”,直到随便按下一个按键,程序跳出循环,执行结束。

    getch()的用法

    头文件包括“conio.h”。

    程序执行到getch(),会保持等待状态,请求用户输入。按下一次按键后,读取一个字符,然后程序继续执行。这个字符可以赋值给其它变量。

    举个例子:

    #include <iostream>
    #include "conio.h"
    using std::cout;
    
    int main()
    {
        int n;
        char c;
        cout<<"Press any key:
    ";
        getch();
        cout<<"You pressed a key.
    ";
        cout<<"Press another key:
    ";
        c = getch();
        cout<<"You pressed "<<c<<"
    ";
        cout<<"Press another key again:
    ";
        n = getch();
        cout<<"You pressed "<<n<<"
    ";    //ASCII
    
        return 0;
    }

    三次输入,按下的都是“a”。最后输出:

    结合以上特点,下面写个小游戏练习键盘输入检测。

     

    练习

    程序运行后,会显示一个密码盘,有0-9总共10个数字,并显示一个能由用户操控的光标(*),开始时居中,形式如下。

    按下a键和d键控制光标分别向左和向右移动(称为key)。密码自由设定,有先后顺序。在下面的例程中,用户需要先向左转到1,再向右转到8,依次打开两道锁。每次打开锁后,会输出一行提示。

    效果:

    两道锁都打开后,按下任意按键退出。

    代码中用了system(“cls”)来清屏,用了Sleep()来让系统等待特定的时间。(引用头文件“windows.h”)

    #include <iostream>
    #include "windows.h"    //for system("cls") and Sleep()
    #include "conio.h"    //for getch()
    #include <string>    //for string
    using std::cout;
    using std::string;
    
    //此处定义光标的图案和密码位置
    const char key = '*';
    const int pw1 = 2;
    const int pw2 = 16;
    
    //显示密码锁的界面
    void show()
    {
        string numbers = "0 1 2 3 4 5 6 7 8 9
    ";
        string lines = "| | | | | | | | | |
    ";
        cout<<numbers;
        cout<<lines;
    }
    
    //输出空格
    void space(int i)
    {
        char space = '';
        for(;i>0;i--)
        {
            cout<<space;
        }
    }
    
    //核对密码
    bool check_key_1(int k)
    {
        return k == pw1;
    }
    
    bool check_key_2(int k)
    {
        return k == pw2;
    }
    
    void move_key()
    {
        int place = 9;            //空格数,定位用
        char p;                   //用户的键盘输入
        int counts = 0;           //解锁次数统计
    
        show();                   //显示界面
        space(place);
        cout<<key;               //定位并显示光标
        p = getch();              //获取键盘输入
    
        int flag = 1;             //用于保持循环的标志
        while(flag)
        {
            system("cls");        //清屏刷新
            show();
            if( p == 'a')
            {
                place--;          //光标左移一格
                space(place);
                cout<<key<<"
    ";
            }
            else if( p == 'd')
            {
                place++;          //光标右移一格
                space(place);
                cout<<key<<"
    ";
            }
            else
            {
                break;            //按下的不是a和d就退出
            };
    
            switch(counts)
            {
            case 0:
            {
                if(check_key_1(place))
                {
                    cout<<"First lock Unlocked!
    ";
                    Sleep(1000);    //等待一秒
                    counts ++;
                }
                else{}
                break;
            }
            case 1:
            {
                if(check_key_2(place))
                {
                    cout<<"Second lock Unlocked!
    ";
                    Sleep(1000);
                    counts ++;
                }
                else{}
                break;
            }
            default:
            {
                cout<<"All locks have been unlocked.
    ";
                cout<<"Press any key to continue...";
                Sleep(1000);
                flag = 0;
                break;
            }
            }
            p = getch();    //获取下一次键盘输入
        }
    }
    
    int main()
    {
        move_key();
        return 0;
    }
  • 相关阅读:
    HTTP请求行、请求头、请求体详解
    json_encode里面经常用到的 JSON_UNESCAPED_UNICODE和JSON_UNESCAPED_SLASHES
    php 使用fsockopen 发送http请求
    PHP与Nginx之间的运行机制以及原理
    用户对动态PHP网页访问过程,以及nginx解析php步骤
    sql优化的几种方法
    mysql锁2
    CentOS 7.4系统优化/安装软件
    Linux基本操作命令
    使用远程管理工具Xshell
  • 原文地址:https://www.cnblogs.com/banmei-brandy/p/11466686.html
Copyright © 2011-2022 走看看