#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#define _WIN32_WINNT 0400
#define WIN32_LEAN_AND_MEAN
typedef struct tagKBDLLHOOKSTRUCT {
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
DWORD dwExtraInfo;
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
DWORD g_main_tid = 0;
HHOOK g_kb_hook = 0;
int flag=0;
int flag2=0;
int flag3=0;
BOOL CALLBACK con_handler (DWORD)
{
PostThreadMessage (g_main_tid, WM_QUIT, 0, 0);//向指定线程post消息
return TRUE;
}
LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
const char *info = NULL;
if(p->vkCode==0x5b)//屏蔽左边的windows键
{
return true;
}
if((p->vkCode==0x90)&&(w==WM_KEYDOWN))//如果按下NumLock键flag3取反
{
keybd_event(0x90,0,0,0);
keybd_event(0x90,0,KEYEVENTF_KEYUP,0);
flag3=!flag3;
return true;
}
if (!flag3)//如果flag3为0
{
if (w == WM_KEYDOWN)
{
info = "key dn";
if(p->vkCode==0x32)
{
keybd_event(0x67,0,0,0);//如果按主键盘的2则按小键盘的7
return true;
}
else if(p->vkCode==0x33)
{
keybd_event(0x68,0,0,0);//按3则按8
return true;
}
else if(p->vkCode==0x23)//如果按下end键
{
if(!flag)//如果flag=0
{
keybd_event(0xdb,0,0,0);//则按下]键
}
else
{
keybd_event(0xdb,0,KEYEVENTF_KEYUP,0);
}
flag=!flag;
return true;
}
else if(p->vkCode==0x24)//如果按下home键
{
if(!flag2)//如果flag2=0
{
keybd_event(0xdd,0,0,0);//则按下[键
}
else
{
keybd_event(0xdd,0,KEYEVENTF_KEYUP,0);
}
flag2=!flag2;
return true;
}
else
{
return CallNextHookEx (g_kb_hook, code, w, l);
}
}
else if (w == WM_KEYUP)//如果是松开某键
{
info = "key up";
if(p->vkCode==0x32)//如果松开的是主键盘的2
{
keybd_event(0x67,0,KEYEVENTF_KEYUP,0);//则松开小键盘的7
return true;
}
else if(p->vkCode==0x33)//如果松开的是主键盘的3
{
keybd_event(0x68,0,KEYEVENTF_KEYUP,0);//则松开小键盘的8
return true;
}
else//其他情况不处理
{
return CallNextHookEx (g_kb_hook, code, w, l);
}
}
else if (w == WM_SYSKEYDOWN)
{
info = "sys key dn";
return CallNextHookEx (g_kb_hook, code, w, l);
}
else if (w == WM_SYSKEYUP)
{
info = "sys key up";
return CallNextHookEx (g_kb_hook, code, w, l);
}
}
return CallNextHookEx (g_kb_hook, code, w, l);
// always call next hook
}
int main (void)
{
printf("\n\n >>> 此程序为魔兽争霸游戏设计 <<<\n\n");
printf(" 1.按end键显示或关闭自己的血量,按home键显示或关闭敌人的血量。\n\n");
printf(" 2.运行此程序后,小键盘的7可用主键盘的2代替,8用3代替。\n\n");
printf(" 3.按numlock键取消以上功能,再按一次启用以上功能。\n\n");
printf(" 4.此程序还屏蔽了左边的win键。\n\n");
printf(" >>>如有问题请登陆理工贴吧www.sduttieba.cn<<<\n\n");
printf(" >>>理工贴吧支持手机访问 wap.sduttieba.cn<<<");
g_main_tid = GetCurrentThreadId ();//获取当前线程一个唯一的线程标识符
SetConsoleCtrlHandler (&con_handler, TRUE);
g_kb_hook = SetWindowsHookEx (13,&kb_proc,GetModuleHandle (NULL),0);//创建钩子
if (g_kb_hook == NULL)
{
fprintf (stderr,"SetWindowsHookEx failed with error %d\n",::GetLastError ());
return 0;
}
// 消息循环是必须的,想知道原因可以查msdn
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);//这个和mfc的消息差不多
};
UnhookWindowsHookEx (g_kb_hook);//卸载钩子
return 0;
}