全局键盘钩子代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;
using System.Reflection;namespace KeyHookDemo
{
public class Wind32Hook
{
//安装钩子原型
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowsHookEx
(
int hookid,
HookPro pfnhook,
IntPtr hinst,
int threadid
); //卸载钩子原型
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool UnhookWindowsHookEx
(
IntPtr hhook
); //回调钩子原型
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr CallNextHookEx
(
IntPtr hhook,
int code,
IntPtr wparam,
IntPtr lparam
); //原型
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]
public static extern void CopyMemory
(
ref KBDLLHOOKSTRUCT Source,
IntPtr Destination, int Length
); public delegate IntPtr HookPro(int nCode, IntPtr wParam, IntPtr lParam); //创建委托,进行回调
static IntPtr hHook = IntPtr.Zero; //创建钩子编号
const int WH_KEYBOARD_LL = 13; //创建键盘钩子类型 GCHandle _hookProcHandle; /*
* 此方法用来响应键盘事件
*/
public IntPtr KEYBOARD_HOOKPRO(int nCode, IntPtr wParam, IntPtr lParam)
{
KBDLLHOOKSTRUCT kb = new KBDLLHOOKSTRUCT();
CopyMemory(ref kb, lParam, 20); //结果就在这里了
//组合键 : ctrl + alt + s
if (kb.vkCode == (int)Keys.S && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt)
{
MessageBox.Show("ok");
UNLOAD_WINDOWS_KETBOARD_HOOK();
SET_WINDOWS_KEYBOARD_HOOK();
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
} /*
* 此方法用来挂钩子
*/
public void SET_WINDOWS_KEYBOARD_HOOK()
{
if (hHook == IntPtr.Zero)
{
HookPro hk = new HookPro(this.KEYBOARD_HOOKPRO);
_hookProcHandle = GCHandle.Alloc(hk);
//挂钩子
hHook = SetWindowsHookEx(
WH_KEYBOARD_LL,
hk,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
0);
if (hHook == IntPtr.Zero)
{
MessageBox.Show("钩子安装不成功!"); // 挂钩子不成功返回值 0
}
}
} /*
* 此方法用来卸载钩子
*/
public void UNLOAD_WINDOWS_KETBOARD_HOOK()
{
if (hHook != IntPtr.Zero)
{
//如果钩子已经挂上则取消钩子,否则不用取消
UnhookWindowsHookEx(hHook);
_hookProcHandle.Free();
hHook = IntPtr.Zero;
}
}
}
//键盘Hook结构函数
[StructLayout(LayoutKind.Sequential)]
public struct KBDLLHOOKSTRUCT
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
然后新建窗口调用:Wind32Hook hook = new Wind32Hook();
hook.SET_WINDOWS_KEYBOARD_HOOK();
程序正常运行,但“钩子安装不成功!!!!!!!我查了很多代码没发现有什么出错,还找了很多资料,也没发现有哪里错了,就是不成功!!高手请指点一下小弟,谢谢!
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;
using System.Reflection;namespace KeyHookDemo
{
public class Wind32Hook
{
//安装钩子原型
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowsHookEx
(
int hookid,
HookPro pfnhook,
IntPtr hinst,
int threadid
); //卸载钩子原型
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool UnhookWindowsHookEx
(
IntPtr hhook
); //回调钩子原型
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr CallNextHookEx
(
IntPtr hhook,
int code,
IntPtr wparam,
IntPtr lparam
); //原型
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]
public static extern void CopyMemory
(
ref KBDLLHOOKSTRUCT Source,
IntPtr Destination, int Length
); public delegate IntPtr HookPro(int nCode, IntPtr wParam, IntPtr lParam); //创建委托,进行回调
static IntPtr hHook = IntPtr.Zero; //创建钩子编号
const int WH_KEYBOARD_LL = 13; //创建键盘钩子类型 GCHandle _hookProcHandle; /*
* 此方法用来响应键盘事件
*/
public IntPtr KEYBOARD_HOOKPRO(int nCode, IntPtr wParam, IntPtr lParam)
{
KBDLLHOOKSTRUCT kb = new KBDLLHOOKSTRUCT();
CopyMemory(ref kb, lParam, 20); //结果就在这里了
//组合键 : ctrl + alt + s
if (kb.vkCode == (int)Keys.S && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt)
{
MessageBox.Show("ok");
UNLOAD_WINDOWS_KETBOARD_HOOK();
SET_WINDOWS_KEYBOARD_HOOK();
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
} /*
* 此方法用来挂钩子
*/
public void SET_WINDOWS_KEYBOARD_HOOK()
{
if (hHook == IntPtr.Zero)
{
HookPro hk = new HookPro(this.KEYBOARD_HOOKPRO);
_hookProcHandle = GCHandle.Alloc(hk);
//挂钩子
hHook = SetWindowsHookEx(
WH_KEYBOARD_LL,
hk,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
0);
if (hHook == IntPtr.Zero)
{
MessageBox.Show("钩子安装不成功!"); // 挂钩子不成功返回值 0
}
}
} /*
* 此方法用来卸载钩子
*/
public void UNLOAD_WINDOWS_KETBOARD_HOOK()
{
if (hHook != IntPtr.Zero)
{
//如果钩子已经挂上则取消钩子,否则不用取消
UnhookWindowsHookEx(hHook);
_hookProcHandle.Free();
hHook = IntPtr.Zero;
}
}
}
//键盘Hook结构函数
[StructLayout(LayoutKind.Sequential)]
public struct KBDLLHOOKSTRUCT
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
然后新建窗口调用:Wind32Hook hook = new Wind32Hook();
hook.SET_WINDOWS_KEYBOARD_HOOK();
程序正常运行,但“钩子安装不成功!!!!!!!我查了很多代码没发现有什么出错,还找了很多资料,也没发现有哪里错了,就是不成功!!高手请指点一下小弟,谢谢!
using (System.Diagnostics.ProcessModule curModule = curProcess.MainModule)
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure,GetModuleHandle(curModule.ModuleName), 0);参考这个