📄 keyboardhook.cpp
字号:
// KeyboardHook.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include <afxdllx.h>
//Add code
#include "ControlHook.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Add code
HWND pHwnd1 = NULL;
HWND pHwnd2 = NULL;
//钩子句柄
HHOOK glhHook = NULL;
//DLL实例句柄
HINSTANCE pHinstance = NULL;
BYTE *SaveData;
int position = 0;
int WIDE = 1000;
int counter = 0;
static AFX_EXTENSION_MODULE KeyboardHookDLL = { NULL, NULL };
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("KEYBOARDHOOK.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(KeyboardHookDLL, hInstance))
return 0;
// Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
// an MFC Regular DLL (such as an ActiveX Control)
// instead of an MFC application, then you will want to
// remove this line from DllMain and put it in a separate
// function exported from this Extension DLL. The Regular DLL
// that uses this Extension DLL should then explicitly call that
// function to initialize this Extension DLL. Otherwise,
// the CDynLinkLibrary object will not be attached to the
// Regular DLL's resource chain, and serious problems will
// result.
new CDynLinkLibrary(KeyboardHookDLL);
//Add code
pHinstance = hInstance;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("KEYBOARDHOOK.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(KeyboardHookDLL);
}
return 1; // ok
}
extern "C" LRESULT WINAPI KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if( nCode >= 0 )
{
position = 0;
//按下F9键时激活钩子
if( wParam == 0X78 && position == 0 )
{
SaveData = (BYTE *)malloc(sizeof(BYTE)*WIDE);
FILE *fp;
fp=fopen("c:\\Key.txt","rb");
for(int i=0;i<WIDE;i++)
{
fread(&SaveData[i], sizeof(BYTE), 1, fp);
}
fclose(fp);
keybd_event(SaveData[counter],0,0,0);
keybd_event(SaveData[counter],0,KEYEVENTF_KEYUP,0);
free(SaveData);
position++;
counter++;
}
}
return CallNextHookEx(glhHook,nCode,wParam,lParam);
}
///////////////////////////////////////////////////////////////
// CControlHook
CControlHook::CControlHook()
{
//Add your code here
}
CControlHook::~CControlHook()
{
if(glhHook)
{
StopHook();
}
}
//安装全局钩子
HHOOK CControlHook::StartHook(HWND hwnd1, HWND hwnd2)
{
pHwnd1 = hwnd1;
pHwnd2 = hwnd2;
//设置键盘钩子
glhHook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,pHinstance,0);
return glhHook;
}
//卸载全局钩子
BOOL CControlHook::StopHook()
{
BOOL bResult=TRUE;
if(glhHook)
{
//卸载键盘钩子
bResult=UnhookWindowsHookEx(glhHook);
}
return bResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -