C++中如何实时监控键盘.docx
《C++中如何实时监控键盘.docx》由会员分享,可在线阅读,更多相关《C++中如何实时监控键盘.docx(12页珍藏版)》请在冰豆网上搜索。
![C++中如何实时监控键盘.docx](https://file1.bdocx.com/fileroot1/2022-12/6/78f66483-4c4f-40c0-9f2e-ebec8f24f989/78f66483-4c4f-40c0-9f2e-ebec8f24f9891.gif)
C++中如何实时监控键盘
如何实时监控键盘的按键
在一个控制程序中,如何能够实时监控键盘的热键?
如设备正在运行,按下键盘的”ESC”来停止程序的运行?
使用消息传递和对键盘消息监视检查即可实现。
下面实例就是通过线程程序运行中监视”ESC”按键和”SPACE”按键
AppMessage1(tagMSG&Msg,bool&Handled)
{
if(Msg.message==WM_KEYDOWN)
{
if(Msg.wParam==VK_SPACE)
{
dw=WaitForSingleObject(g_hMutex,INFINITE);
//等待互斥量信号
if(dw==WAIT_OBJECT_0)
{
KVar.b1=true;
}
ReleaseMutex(g_hMutex);
}
if(Msg.wParam==VK_ESCAPE)
{
dw=WaitForSingleObject(g_hMutex,INFINITE);
//等待互斥量信号
if(dw==WAIT_OBJECT_0)
KVar.b2=true;
ReleaseMutex(g_hMutex);
}
}
}
在事例程序中,使用线程监控按键,并置变量
structKuseVar{
boolb1;//记录ESC按键按下
boolb2;//记录SPACE按键按下
};
externKuseVarKVar;
示例中,”ESC”按键按下时,AppMessage1函数通过Msg.message检查到是否有按键按下,如果有则通过Msg.wParam检查是否有”ESC”按键按下,如有则置structKuseVar中的b1为true;如有”SPACE”按键按下,则置structKuseVar中的b2为true。
Timer1和timer2分别检查structKuseVar中的b1、b2来显示按键按下或是恢复。
图1为源程序设计时的窗体布置,图2为程序运行时的界面。
此时按下”ESC”或”SPACE”按键,Label1和Label2显示按键状态。
按下”打开新窗口”后弹出一个新窗口,此时按下”ESC”或”SPACE”按键,Label1和Label2仍可显示按键的状态。
图1
图2
最重要的一步:
Application->OnMessage=b->AppMessage1;
把改代码放在如下执行
__fastcallTForm1:
:
TForm1(TComponent*Owner)
:
TForm(Owner)
{
Application->OnMessage=b->AppMessage1;
KVar.b1=false;
KVar.b2=false;
g_hMutex=CreateMutex(NULL,FALSE,NULL);
}
以下为C++Builder的源代码
//---------------------------------------------------------------------------
#ifndefUnit1H
#defineUnit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
classTForm1:
publicTForm
{
__published:
//IDE-managedComponents
TImage*Image1;
TCheckBox*CheckBox1;
TCheckBox*CheckBox2;
TButton*Button1;
TButton*Button2;
TLabel*Label1;
TTimer*Timer1;
TLabel*Label2;
TTimer*Timer2;
TButton*Button3;
void__fastcallCheckBox1Click(TObject*Sender);
void__fastcallCheckBox2Click(TObject*Sender);
void__fastcallTimer1Timer(TObject*Sender);
void__fastcallTimer2Timer(TObject*Sender);
void__fastcallButton3Click(TObject*Sender);
private:
//Userdeclarations
public:
//Userdeclarations
__fastcallTForm1(TComponent*Owner);
};
//---------------------------------------------------------------------------
externPACKAGETForm1*Form1;
structKuseVar{
boolb1;
boolb2;
};
externDWORDdw;
externHANDLEg_hMutex;
externKuseVarKVar;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include
#pragmahdrstop
#include
#include"Unit1.h"
#include"Unit2.h"
#include
#include
#include"Unit3.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TForm1*Form1;
Pubble*b,*c;
intk=0;
intx,y;
KuseVarKVar;
DWORDdw;
HANDLEg_hMutex=NULL;
//---------------------------------------------------------------------------
__fastcallTForm1:
:
TForm1(TComponent*Owner)
:
TForm(Owner)
{
b=newPubble(clRed,x,y);
Application->OnMessage=b->AppMessage1;
KVar.b1=false;
KVar.b2=false;
g_hMutex=CreateMutex(NULL,FALSE,NULL);
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
CheckBox1Click(TObject*Sender)
{
if(CheckBox1->Checked)
b->Resume();
else
b->Suspend();
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
CheckBox2Click(TObject*Sender)
{
if(CheckBox2->Checked)
c->Resume();
else
c->Suspend();
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
Timer1Timer(TObject*Sender)
{
dw=WaitForSingleObject(g_hMutex,INFINITE);
//等待互斥量信号
if(dw==WAIT_OBJECT_0)
{
if(KVar.b1==true)
{
Form1->Label1->Caption="SPACE键按下";
KVar.b1=false;
}
if(KVar.b2==true)
{
Form1->Label2->Caption="ESC键按下";
KVar.b2=false;
}
}
ReleaseMutex(g_hMutex);
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
Timer2Timer(TObject*Sender)
{
dw=WaitForSingleObject(g_hMutex,INFINITE);
//等待互斥量信号
if(dw==WAIT_OBJECT_0)
{
if(KVar.b1==false)
{
Form1->Label1->Caption="SPACE键恢复";
}
if(KVar.b2==false)
{
Form1->Label2->Caption="ESC键恢复";
}
}
ReleaseMutex(g_hMutex);
}
//---------------------------------------------------------------------------
void__fastcallTForm1:
:
Button3Click(TObject*Sender)
{
Form3->Show();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndefUnit2H
#defineUnit2H
//---------------------------------------------------------------------------
#include
#include
//---------------------------------------------------------------------------
classPubble:
publicTThread
{
private:
TColorcolor;
protected:
void__fastcallExecute();
void__fastcallPaint();
public:
__fastcallPubble(boolCreateSuspended);
__fastcallPubble(TColorcolor,intXPos,intYPos,boolCreateSuspended=true);
void__fastcallAppMessage1(tagMSG&Msg,bool&Handled);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include
#pragmahdrstop
#include
#include"Unit2.h"
#include"Unit1.h"
#include
#include
#pragmapackage(smart_init)
//---------------------------------------------------------------------------
//Important:
MethodsandpropertiesofobjectsinVCLcanonlybe
//usedinamethodcalledusingSynchronize,forexample:
//
//Synchronize(UpdateCaption);
//
//whereUpdateCaptioncouldlooklike:
//
//void__fastcallBubble:
:
UpdateCaption()
//{
//Form1->Caption="Updatedinathread";
//}
//---------------------------------------------------------------------------
__fastcallPubble:
:
Pubble(boolCreateSuspended)
:
TThread(CreateSuspended)
{
}
//-----------------------------------------------
__fastcallPubble:
:
Pubble(TColormycolor,intXPos,intYPos,boolCreateSuspended)
:
TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void__fastcallPubble:
:
Execute()
{
//----Placethreadcodehere----
}
//---------------------------------------------------------------------------
void__fastcallPubble:
:
AppMessage1(tagMSG&Msg,bool&Handled)
{
if(Msg.message==WM_KEYDOWN)
{
if(Msg.wParam==VK_SPACE)
{
dw=WaitForSingleObject(g_hMutex,INFINITE);
//等待互斥量信号
if(dw==WAIT_OBJECT_0)
{
KVar.b1=true;
}
ReleaseMutex(g_hMutex);
}
if(Msg.wParam==VK_ESCAPE)
{
dw=WaitForSingleObject(g_hMutex,INFINITE);
//等待互斥量信号
if(dw==WAIT_OBJECT_0)
KVar.b2=true;
ReleaseMutex(g_hMutex);
}
}
}
void__fastcallPubble:
:
Paint()
{
}
//------------------------------------------
//---------------------------------------------------------------------------
#ifndefUnit3H
#defineUnit3H
//---------------------------------------------------------------------------
#include
#include
#include
#include
//---------------------------------------------------------------------------
classTForm3:
publicTForm
{
__published:
//IDE-managedComponents
private:
//Userdeclarations
public:
//Userdeclarations
__fastcallTForm3(TComponent*Owner);
};
//---------------------------------------------------------------------------
externPACKAGETForm3*Form3;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include
#pragmahdrstop
#include"Unit3.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TForm3*Form3;
//---------------------------------------------------------------------------
__fastcallTForm3:
:
TForm3(TComponent*Owner)
:
TForm(Owner)
{
}
//---------------------------------------------------------------------------