底层键盘钩子
/*========================================================================
文件: kbevent.h
说明:全局消息钩子
时间:2005-03-20
编写:oshj || oshj@21cn.com
环境:VC6.0/Win2000 Pro/SP4/1024*768
特别说明:NT5.0以上操作系统才能支持,WIN9X不支持!
=========================================================================*/
//-------------------------------------------------------------------------
#ifndef _INC_KEEVENT
#define _INC_KEEVENT
#include <windows.h>
#include <stdio.h>
//-------------------------------------------------------------------------
#define DLLEXPORT extern "C" __declspec(dllexport)
//-------------------------------------------------------------------------
#define MAX_TEXTLEN 1024
#define UM_GETTEXT WM_USER + 0x392
//-------------------------------------------------------------------------
//低级键盘钩子回调函数
LRESULT CALLBACK LowLevelKBProc(int nCode, WPARAM wParam, LPARAM lParam);
//按ctrl+alt+del时系统管理器无效
void RegKeyEnable(BOOL bEnable=TRUE);
///////////////////////////////////////////////////////////////////////////
//键盘空虚时间回调函数
LRESULT CALLBACK MyKbdHook(int code, WPARAM wParam, LPARAM lParam);
//鼠标空虚时间回调函数
LRESULT CALLBACK MyMouseHook(int code, WPARAM wParam, LPARAM lParam);
//-------------------------------------------------------------------------
#endif // _INC_KEEVENT
//.cpp
//-------------------------------------------------------------------------
#define _WIN32_WINNT 0x0500 //仅NT5.0以上系统可用
#include "kbevent.h"
//-------------------------------------------------------------------------
// Magic registry key/value for "Remove Task Manager" policy.
#define HKCU HKEY_CURRENT_USER
LPCTSTR KEY_DisableTaskMgr =
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
LPCTSTR VAL_DisableTaskMgr = "DisableTaskMgr";
//-------------------------------------------------------------------------
#pragma data_seg(".sdata")
HHOOK g_hHookKeybd = NULL;
HHOOK g_hHookMouse = NULL;
DWORD g_dwLastInputTick = 0;
#pragma data_seg()
#pragma comment(linker,"-section:.sdata,rws")
//-------------------------------------------------------------------------
// 全局变量
HINSTANCE g_hInstance = NULL; // 模块实例句柄
HHOOK g_hHook = NULL; // 钩子句柄
//-------------------------------------------------------------------------
//dll main
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
// 保存模块实例句柄
g_hInstance = (HINSTANCE)hinstDLL;
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
if( NULL!=g_hHook )
UnhookWindowsHookEx( g_hHook );
if( NULL!=g_hHookKeybd )
UnhookWindowsHookEx( g_hHookKeybd );
if( NULL!=g_hHookMouse )
UnhookWindowsHookEx( g_hHookMouse );
break;
}
return TRUE;
}
//-------------------------------------------------------------------------
// 底层键盘钩子函数
LRESULT CALLBACK LowLevelKBProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
PKBDLLHOOKSTRUCT p;
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
p = (PKBDLLHOOKSTRUCT)lParam;
if( //(p->vkCode==VK_TAB) ||
(p->vkCode==VK_ESCAPE) ||
(p->vkCode==VK_LWIN) ||
(p->vkCode==VK_RWIN) ||
(p->vkCode==VK_CONTROL) ||
(p->vkCode==VK_SHIFT) ||
(p->vkCode==VK_DELETE) ||
( (p->vkCode==VK_F4) && ((p->flags & LLKHF_ALTDOWN)!=0) ) || //ALT+F4
( (p->vkCode==VK_TAB) && ((p->flags & LLKHF_ALTDOWN)!=0) ) || //ALT+TAB
( (p->vkCode==VK_ESCAPE) && ((p->flags & LLKHF_ALTDOWN)!=0) ) || //ALT+ESC
( (p->vkCode==VK_ESCAPE) && ((GetKeyState(VK_CONTROL) & 0x8000)!=0) ) || //CTRL+ESC
( (p->vkCode==VK_DELETE) && ((p->flags & LLKHF_ALTDOWN)!=0) && ((GetKeyState(VK_CONTROL) & 0x8000)!=0) ) //CTRL+ESC
)
{
return TRUE;
}
break;
}
}
return ::CallNextHookEx( g_hHook, nCode, wParam, lParam );
}
//-------------------------------------------------------------------------
//开始钩子
DLLEXPORT BOOL WINAPI StartKBEvent()
{
//如果已经安装键盘钩子则返回 FALSE
if ( NULL!=g_hHook ) return FALSE;
RegKeyEnable(FALSE);
//安装底层键盘钩子
//NT5.0以上操作系统才能支持
//WIN98不支持!
g_hHook = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKBProc, g_hInstance, NULL );
if ( g_hHook==NULL) return FALSE;
return TRUE;
}
//-------------------------------------------------------------------------
//结束钩子
DLLEXPORT BOOL WINAPI StopKBEvent()
{
RegKeyEnable(TRUE);
//卸载钩子
if( UnhookWindowsHookEx(g_hHook)==0 )
return FALSE;
g_hHook = NULL;
return TRUE;
}
//-------------------------------------------------------------------------
//按ctrl+alt+del时系统管理器无效
void RegKeyEnable(BOOL bEnable)
{
HKEY hk;
if( RegOpenKey(HKCU, KEY_DisableTaskMgr,&hk)!=ERROR_SUCCESS )
{
RegCreateKey(HKCU, KEY_DisableTaskMgr, &hk);
}
//
if( TRUE==bEnable )
{
RegDeleteValue(hk,VAL_DisableTaskMgr);
}
else
{
DWORD val=1;
RegSetValueEx(hk, VAL_DisableTaskMgr, NULL,REG_DWORD, (BYTE*)&val, sizeof(val));
}
}
///////////////////////////////////////////////////////////////////////////
//以下是获取键盘鼠标空闲时间的钩子部分
//-------------------------------------------------------------------------
//键盘空闲时间回调函数
LRESULT CALLBACK MyKbdHook(int code, WPARAM wParam, LPARAM lParam)
{
if( code==HC_ACTION )
{
g_dwLastInputTick = GetTickCount();
PKBDLLHOOKSTRUCT p;
p = (PKBDLLHOOKSTRUCT) lParam;
//MessageBox(0,"111","aaa",MB_OK);
//Z键连续按下2次以上
if( (p->vkCode==VK_ESCAPE) )// && ((p->flags & KF_REPEAT)>=2) )
{
//MessageBox(0,"yes","aaa",MB_OK);
return TRUE;
}
}
return ::CallNextHookEx( g_hHookKeybd, code, wParam, lParam );
}
//-------------------------------------------------------------------------
//鼠标空闲时间回调函数
LRESULT CALLBACK MyMouseHook(int code, WPARAM wParam, LPARAM lParam)
{
if (code==HC_ACTION)
{
g_dwLastInputTick = GetTickCount();
}
return ::CallNextHookEx( g_hHookMouse, code, wParam, lParam );
}
//-------------------------------------------------------------------------
//开始钩子
DLLEXPORT BOOL WINAPI IdleInit()
{
if( NULL!=g_hHookKeybd && NULL!=g_hHookMouse ) return FALSE;
g_hHookKeybd = SetWindowsHookEx( WH_KEYBOARD, MyKbdHook, g_hInstance, 0 );
g_hHookMouse = SetWindowsHookEx( WH_MOUSE, MyMouseHook, g_hInstance, 0 );
g_dwLastInputTick = GetTickCount();
return TRUE;
}
//-------------------------------------------------------------------------
//结束钩子
DLLEXPORT BOOL WINAPI IdleExit()
{
if( NULL==g_hHookKeybd && NULL==g_hHookMouse ) return FALSE;
UnhookWindowsHookEx( g_hHookKeybd );
UnhookWindowsHookEx( g_hHookMouse );
return TRUE;
}
//-------------------------------------------------------------------------
//获得空闲时间
DLLEXPORT DWORD IdleGetLastTime()
{
return g_dwLastInputTick;
}