如何在C#中使用全局鼠标、键盘Hook

  1. 云栖社区>
  2. 博客>
  3. 正文

如何在C#中使用全局鼠标、键盘Hook

橘子红了呐 2017-11-10 00:15:00 浏览806
展开阅读全文

今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全局钩子呢?于是开始到网上搜索,好不容易找到一篇,318804 - HOW TO: Set a Windows Hook in Visual C# .NET,里面详细的说明了如何使用鼠标钩子捕获鼠标的移动等,可是,它只能在Application里起作用,出了Application就没用了,就是说它还是没有实现全局钩子,而且文章结尾处说:“Global Hooks are not supported in the .NET Framework...”,这可怎么办呢?

别担心,办法总是有的,经过一番摸索以后,发现WH_KEYBORAD_LL和WH_MOUSE_LL这两个low-level的hook可以被安装成全局的,这就好办了,我们不妨用这两个low-level的hook替换掉WH_KEYBORAD和WH_MOUSE,于是开始测试。结果成功了,在C#里实现了全局钩子。

我们来看一下主要代码段。示例源码下载地址请访问我的网站:http://www.vczx.com/article/show.php?id=1672

首先倒入所需要的windows函数,主要有三个,SetWindowsHookEX用来安装钩子,UnhookWindowsHookEX用来卸载钩子以及CallNextHookEX用来将hook信息传递到链表中下一个hook处理过程。

None.gif[DllImport("user32.dll", CharSet = CharSet.Auto,
None.gif           CallingConvention 
= CallingConvention.StdCall, SetLastError = true)]
None.gif        
private static extern int SetWindowsHookEx(
None.gif            
int idHook,
None.gif            HookProc lpfn,
None.gif            IntPtr hMod,
None.gif            
int dwThreadId);
None.gif
None.gif[DllImport(
"user32.dll", CharSet = CharSet.Auto,
None.gif            CallingConvention 
= CallingConvention.StdCall, SetLastError = true)]
None.gif        
private static extern int UnhookWindowsHookEx(int idHook);
None.gif
None.gif[DllImport(
"user32.dll", CharSet = CharSet.Auto,
None.gif             CallingConvention 
= CallingConvention.StdCall)]
None.gif        
private static extern int CallNextHookEx(
None.gif            
int idHook,
None.gif            
int nCode,
None.gif            
int wParam,
None.gif            IntPtr lParam);

下面是有关这两个low-level hook在Winuser.h中的定义:

ExpandedBlockStart.gif/// <summary>
InBlock.gif        
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events.
ExpandedBlockEnd.gif        
/// </summary> 

None.gif        private const int WH_MOUSE_LL       = 14;
ExpandedBlockStart.gif        
/// <summary>
InBlock.gif        
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard  input events.
ExpandedBlockEnd.gif        
/// </summary> 

None.gif        private const int WH_KEYBOARD_LL    = 13;

在安装全局钩子的时候,我们就要做替换了,将WH_MOUSE和WH_KEYBORAD分别换成WH_MOUSE_LL和WH_KEYBORAD_LL:

None.gif//install hook
None.gif
                hMouseHook = SetWindowsHookEx(
None.gif                    WH_MOUSE_LL, 
//原来是WH_MOUSE
None.gif
                    MouseHookProcedure,
None.gif                    Marshal.GetHINSTANCE(
None.gif                        Assembly.GetExecutingAssembly().GetModules()[
0]),
None.gif                    
0);
None.gif
None.gif
//install hook
None.gif
                hKeyboardHook = SetWindowsHookEx(
None.gif                    WH_KEYBOARD_LL, 
//原来是WH_KEYBORAD
None.gif
                    KeyboardHookProcedure,
None.gif                    Marshal.GetHINSTANCE(
None.gif                    Assembly.GetExecutingAssembly().GetModules()[
0]),
None.gif                    
0);
 
  这样替换了之后,我们就可以实现全局钩子了,而且,不需要写DLL。看一下程序运行情况:

HookTest.jpg

下面是关于鼠标和键盘的两个Callback函数:

None.gifprivate int MouseHookProc(int nCode, int wParam, IntPtr lParam)
ExpandedBlockStart.gif        
{
InBlock.gif            
// if ok and someone listens to our events
InBlock.gif
            if ((nCode >= 0&& (OnMouseActivity != null))
ExpandedSubBlockStart.gif            
{
InBlock.gif                
//Marshall the data from callback.
InBlock.gif
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
InBlock.gif
InBlock.gif                
//detect button clicked
InBlock.gif
                MouseButtons button = MouseButtons.None;
InBlock.gif                
short mouseDelta = 0;
InBlock.gif                
switch (wParam)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    
case WM_LBUTTONDOWN:
InBlock.gif                        
//case WM_LBUTTONUP: 
InBlock.gif                        
//case WM_LBUTTONDBLCLK: 
InBlock.gif
                        button = MouseButtons.Left;
InBlock.gif                        
break;
InBlock.gif                    
case WM_RBUTTONDOWN:
InBlock.gif                        
//case WM_RBUTTONUP: 
InBlock.gif                        
//case WM_RBUTTONDBLCLK: 
InBlock.gif
                        button = MouseButtons.Right;
InBlock.gif                        
break;
InBlock.gif                    
case WM_MOUSEWHEEL:
InBlock.gif                        
//If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta. 
InBlock.gif                        
//One wheel click is defined as WHEEL_DELTA, which is 120. 
InBlock.gif                        
//(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
InBlock.gif
                        mouseDelta = (short)((mouseHookStruct.mouseData >> 16& 0xffff);
InBlock.gif                        
//TODO: X BUTTONS (I havent them so was unable to test)
InBlock.gif                        
//If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, 
InBlock.gif                        
//or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, 
InBlock.gif                        
//and the low-order word is reserved. This value can be one or more of the following values. 
InBlock.gif                        
//Otherwise, mouseData is not used. 
InBlock.gif
                        break;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
//double clicks
InBlock.gif
                int clickCount = 0;
InBlock.gif                
if (button != MouseButtons.None)
InBlock.gif                    
if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2;
InBlock.gif                    
else clickCount = 1;
InBlock.gif
InBlock.gif                
//generate event 
InBlock.gif
                 MouseEventArgs e = new MouseEventArgs(
InBlock.gif                                                    button,
InBlock.gif                                                    clickCount,
InBlock.gif                                                    mouseHookStruct.pt.x,
InBlock.gif                                                    mouseHookStruct.pt.y,
InBlock.gif                                                    mouseDelta);
InBlock.gif                
//raise it
InBlock.gif
                OnMouseActivity(this, e);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//call next hook
InBlock.gif
            return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
ExpandedBlockEnd.gif        }

None.gif
None.gifprivate int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
ExpandedBlockStart.gif        
{
InBlock.gif            
//indicates if any of underlaing events set e.Handled flag
InBlock.gif
            bool handled = false;
InBlock.gif            
//it was ok and someone listens to events
InBlock.gif
            if ((nCode >= 0&& (KeyDown != null || KeyUp != null || KeyPress != null))
ExpandedSubBlockStart.gif            
{
InBlock.gif                
//read structure KeyboardHookStruct at lParam
InBlock.gif
                KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
InBlock.gif                
//raise KeyDown
InBlock.gif
                if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
ExpandedSubBlockStart.gif                
{
InBlock.gif                    Keys keyData 
= (Keys)MyKeyboardHookStruct.vkCode;
InBlock.gif                    KeyEventArgs e 
= new KeyEventArgs(keyData);
InBlock.gif                    KeyDown(
this, e);
InBlock.gif                    handled 
= handled || e.Handled;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
// raise KeyPress
InBlock.gif
                if (KeyPress != null && wParam == WM_KEYDOWN)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    
bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80== 0x80 ? true : false);
InBlock.gif                    
bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);
InBlock.gif
InBlock.gif                    
byte[] keyState = new byte[256];
InBlock.gif                    GetKeyboardState(keyState);
InBlock.gif                    
byte[] inBuffer = new byte[2];
InBlock.gif                    
if (ToAscii(MyKeyboardHookStruct.vkCode,
InBlock.gif                              MyKeyboardHookStruct.scanCode,
InBlock.gif                              keyState,
InBlock.gif                              inBuffer,
InBlock.gif                              MyKeyboardHookStruct.flags) 
== 1)
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        
char key = (char)inBuffer[0];
InBlock.gif                        
if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);
InBlock.gif                        KeyPressEventArgs e 
= new KeyPressEventArgs(key);
InBlock.gif                        KeyPress(
this, e);
InBlock.gif                        handled 
= handled || e.Handled;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
// raise KeyUp
InBlock.gif
                if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
ExpandedSubBlockStart.gif                
{
InBlock.gif                    Keys keyData 
= (Keys)MyKeyboardHookStruct.vkCode;
InBlock.gif                    KeyEventArgs e 
= new KeyEventArgs(keyData);
InBlock.gif                    KeyUp(
this, e);
InBlock.gif                    handled 
= handled || e.Handled;
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//if event handled in application do not handoff to other listeners
InBlock.gif
            if (handled)
InBlock.gif                
return 1;
InBlock.gif            
else
InBlock.gif                
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
ExpandedBlockEnd.gif        }





   本文转自loose_went博客园博客,原文链接:http://www.cnblogs.com/michaelxu/archive/2006/09/22/511557.html,如需转载请自行联系原作者





网友评论

登录后评论
0/500
评论
橘子红了呐
+ 关注