Win32命令行应用,+ReadConsoleInput()得到键盘VK_CODE

BOOL ReadConsoleInput(
  HANDLE hConsoleInput, //输入句柄
  PINPUT_RECORD lpBuffer, //指向INPUT_RECORD结构体(数组)的指针
  DWORD nLength, //上面那个结构体的大小
  LPDWORD lpNumberOfEventsRead //实际读入输入内容的个数
);
#pragma once
#include <Windows.h>class GohanConsoleHelper
{
    HANDLE _hIn;
    HANDLE _hOut;
    INPUT_RECORD _InRec;
    DWORD _NumRead;
public:
    WORD VKey;
    GohanConsoleHelper(void){
        _hIn = GetStdHandle(STD_INPUT_HANDLE);
        _hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        VKey=0;
    }
    bool ReadOneInput()
    {
        return 0!=ReadConsoleInput(_hIn,&_InRec,1,&_NumRead);
    }
    bool ReadOneInput(INPUT_RECORD& InRec)
    {
        return 0!=ReadConsoleInput(_hIn,&InRec,1,&_NumRead);
    }
    DWORD ReadKeyDown()
    {
        if(!ReadConsoleInput(_hIn,&_InRec,1,&_NumRead))
            return 0;
        if(_InRec.EventType!=KEY_EVENT)
            return 0;
        if(_InRec.Event.KeyEvent.bKeyDown > 0)
            return 0;
        VKey = _InRec.Event.KeyEvent.wVirtualKeyCode;
        return VKey;
    }
    DWORD ReadKeyPush()
    {
        if(!ReadConsoleInput(_hIn,&_InRec,1,&_NumRead))
            return 0;
        if(_InRec.EventType!=KEY_EVENT)
            return 0;
        if(_InRec.Event.KeyEvent.bKeyDown == 0)
            return 0;
        VKey = _InRec.Event.KeyEvent.wVirtualKeyCode;
        return VKey;
    }
public:
    ~GohanConsoleHelper(void){}
};

#include <windows.h>
#include <iostream>
#include "GohanConsoleHelper.h"
using namespace std;int main()
{
    GohanConsoleHelper gch;
    while (true)
    {
        if(gch.ReadKeyPush()!=0) //使用ReadKeyDown()捕获按键弹起的VK_CODE
        {
            if(gch.VKey != VK_ESCAPE)
                cout<<"VK_CODE == "<<gch.VKey<<endl;
            else {
                cout<<"Bye~~"<<endl;
                break;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值