C++控制台程序,windows环境下响应按键消息实现一
2014-01-25 17:39
423 查看
转自:http://blog.csdn.net/aizquan/article/details/6869806
[cpp] view
plaincopy
BOOL ReadConsoleInput(
HANDLE hConsoleInput, //输入句柄
PINPUT_RECORD lpBuffer, //指向INPUT_RECORD结构体(数组)的指针
DWORD nLength, //上面那个结构体的大小
LPDWORD lpNumberOfEventsRead //实际读入输入内容的个数
);
[cpp] view
plaincopy
#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){}
};
[cpp] view
plaincopy
#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;
}
另外,钩子实现请看:http://blog.csdn.net/gamewyd/article/details/6868290
[cpp] view
plaincopy
BOOL ReadConsoleInput(
HANDLE hConsoleInput, //输入句柄
PINPUT_RECORD lpBuffer, //指向INPUT_RECORD结构体(数组)的指针
DWORD nLength, //上面那个结构体的大小
LPDWORD lpNumberOfEventsRead //实际读入输入内容的个数
);
[cpp] view
plaincopy
#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){}
};
[cpp] view
plaincopy
#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;
}
另外,钩子实现请看:http://blog.csdn.net/gamewyd/article/details/6868290
相关文章推荐
- [C/C++]_[Linux,Windows,MacOSX控制台即时响应按键消息]
- [C/C++]_[Linux,Windows,MacOSX控制台即时响应按键消息]
- C++控制台程序实现暂停功能
- windows C++ 控制台程序不显示黑框的办法
- Windows下更改C/C++控制台程序文本输出颜色
- windows环境VS2015编译TensorFlow C++程序完全攻略
- Windows用户自定义消息实现程序间通信
- Windows控制台下响应键盘消息
- C++/Php/Python/Shell 程序按行读取文件或者控制台的实现
- c++控制台程序实现定时器
- 控制台如何像windows程序那样处理消息事件
- 如何实现程序的重新启动(windows环境下)
- 实现按键C++版的滚轮消息按键
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- Windows环境C/C++ 获取当前执行程序路径的两个函数
- windows下如何建立eclipse开发C++程序环境
- windows 控制台程序 响应 Ctrl+C
- 如何让自己的应用程序,控制台程序,windows 服务收到操作系统注销或者重启的消息
- windows环境中使用VC的nmake和makefile编译c++程序