炒菜式实操:
1.准备好用到的API:
/// <summary>
/// 安装钩子
/// </summary>
/// <param name="idHook"></param>
/// <param name="lpfn">指向相应的挂钩处理过程.若参数dwThreadId为0或者指示了一个其他进程创建的线程之标识符,则参数lpfn必须指向一个动态链接中的挂钩处理过程.否则,参数lpfn可以指向一个与当前进程相关的代码中定义的挂钩处理过程。</param>
/// <param name="hInstance">指示了一个动态链接的句柄,该动态连接库包含了参数lpfn 所指向的挂钩处理过程.若参数dwThreadId指示的线程由当前进程创建,并且相应的挂钩处理过程定义于当前进程相关的代码中,则参数hMod必须被设置为NULL(0)。</param>
/// <param name="threadId">指示了一个线程标识符,挂钩处理过程与线程相关.若此参数值为0,则该挂钩处理过程与所有现存的线程相关。</param>
/// <returns>若此函数执行成功,则返回值就是该挂钩处理过程的句柄;若此函数执行失败,则返回值为NULL(0).若想获得更多错误信息,请调用GetLasError函数。</returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetWindowsHookEx(WH_Codes idHook, HookProc lpfn,IntPtr pInstance, int threadId);
/// <summary>
/// 卸载钩子
/// </summary>
/// <param name="idHook"></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
/// <summary>
/// 传递钩子
/// </summary>
/// <param name="idHook"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode,
Int32 wParam, IntPtr lParam);
}
2.全局参数:
//键盘钩子句柄
private IntPtr m_pKeyboardHook = IntPtr.Zero;
//WH_Codes为钩子类型,其他钩子参考文章最后链接。
public enum WH_Codes : int
{
/// <summary>
/// 底层键盘钩子
/// </summary>
WH_KEYBOARD_LL = 13,
/// <summary>
/// 底层鼠标钩子
/// </summary>
WH_MOUSE_LL = 14
}
/// <summary>
/// 钩子委托声明
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
//钩子委托
private HookProc m_KeyboardHookProcedure;
//声明键盘钩子的封送结构类型
[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo;
}
3.安装全局钩子:
IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);
//KeyboardHookProc为钩子业务逻辑
m_KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//m_pKeyboardHook为返回钩子句柄,后期卸载钩子需要用到
m_pKeyboardHook = Win32API.SetWindowsHookEx(WH_Codes.WH_KEYBOARD_LL, m_KeyboardHookProcedure, pInstance, 0);
//判断钩子安装状态,0安装失败,不为0即成功
if (m_pKeyboardHook == IntPtr.Zero)
{
richTextBox1.Text = m_pKeyboardHook.ToString() + "\n钩子安装失败!\n";
}
else
{
richTextBox1.Text = m_pKeyboardHook.ToString() + "\n钩子安装成功!\n";
}
4.钩子处理:
/// <summary>
/// 键盘钩子处理函数
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
/// <remarks></remarks>
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
//其他业务
//最后返回1,随即全键盘禁用
return 1;
}
//例子,禁用Window键
/// <summary>
/// 键盘钩子处理函数
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
/// <remarks></remarks>
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
KeyboardHookStruct m = (KeyboardHookStruct)Marshal.PtrToStructure(lParam,typeof(KeyboardHookStruct));
if (m.vkCode == (int)Keys.LWin)
{
richTextBox1.Text = "点击了“Win键”";
return 1;
}
else
{
return Win32API.CallNextHookEx(m_pKeyboardHook, nCode, wParam, lParam);
}
}
5.卸载钩子:
Win32API.UnhookWindowsHookEx(m_pKeyboardHook);
参考文章:
https://www.cnblogs.com/hocylan/archive/2008/01/14/1038390.html