怎样记录小键盘的数字?

binglingbanglang 2004-03-09 07:47:13
怎样实现小键盘的0~9的击键记录?
...全文
85 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjy1001 2004-03-23
  • 打赏
  • 举报
回复
给一个记录键盘的全局钩子吧

//---------------------------------------------------------------------------

#pragma option -zRShareSEG // 改变缺省数据段名
#pragma option -zTShareCLASS // 改变缺省数据类名

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused

#pragma data_seg("SharedVar")

#pragma data_seg()


HHOOK NewHook=NULL; // 存放新钩子句柄
int LoadCount=0; // DLL装入次数计数
int DownCount=0; // 键盘按下的次数.
int DownCountM=0; // 鼠标按下的次数.
HINSTANCE DllHinst;
POINT MouseLoc;
KBDLLHOOKSTRUCT *pkbhs;
char keyWord;

__declspec(dllexport) LRESULT CALLBACK KeyHook(int code, WPARAM wParam, LPARAM lParam);

extern "C" __declspec(dllexport) int __stdcall GetMouse(int &js);
extern "C" __declspec(dllexport) int __stdcall GetKey(char &key,int &js);
extern "C" __declspec(dllexport) int __stdcall EnableHook(bool flag); //导出函数EnableHook()

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
DllHinst=hinst;

if (reason==DLL_PROCESS_ATTACH) // DLL入口
{
LoadCount+=1; // 装入计数
}
else
{
if (reason==DLL_PROCESS_DETACH) // DLL出口处理
{
LoadCount-=1;
}
}

return 1;
}
//---------------------------------------------------------------------------
//键盘钩子 回送 键盘键虚拟码
LRESULT CALLBACK KeyHook(int code, WPARAM wParam, LPARAM lParam)
{
EVENTMSG *keyMSG=(EVENTMSG *)lParam;
if(keyMSG->message==WM_KEYUP)
{
keyWord = (char)(keyMSG->paramL);
DownCount++;

}
if(keyMSG->message== WM_LBUTTONDOWN )
{
DownCountM++;
}

return CallNextHookEx(NewHook,
code, wParam, lParam);
}
//-----------------------------
int __stdcall GetMouse(int &js) // DLL导出函数GetMouse()
{
js = DownCountM; // 送出鼠标 按下的次数。
return 1;
}
//--------------------------
int __stdcall GetKey(char &key,int &js) // DLL导出函数GetKey()
{
key = keyWord; // 送出键盘虚拟码
js = DownCount; // 送出键盘 按下的次数。
return 1;
}

//-----------------------------
int __stdcall EnableHook(bool flag) // 导出函数EnableHook()
{
if(flag)
{
if (NewHook==NULL){ // 安装新钩子
NewHook=SetWindowsHookEx(WH_JOURNALRECORD,
(HOOKPROC)KeyHook,
DllHinst,0);
return (NewHook == NULL);
}
}
else
{
if (NewHook!=NULL)
{
UnhookWindowsHookEx(NewHook);
NewHook=NULL; // 卸掉新钩子
return 0;
}

}
return 1;
}

.def文件
SEGMENTS ShareSEG CLASS 'ShareCLASS' SHARED
yjy1001 2004-03-20
  • 打赏
  • 举报
回复
全局的还是自己程序?!

自己的程序 就太容易了点吧

全局的用钩子

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧