日期:2014-05-17 浏览次数:21163 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate int HookProc(int nCode, int wParam, IntPtr lParam);//定义钩子过程委托(钩子函数原型) [DllImport("user32.dll")] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadID); //卸载钩子 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnHookWindowsHookEx(int idHook); //调用下一个钩子 [DllImport("user32.dll")] public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); //返回当前线程ID值 [DllImport("kernel32.dll")] public static extern int GetCurrentThreadId(); //返回指定模块的句柄 [DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string name); #region HookProc KeyBoardProcedure; static int hHook = 0;//钩子函数的句柄 public const int WH_KEYBOARD = 13; //钩子结构函数 public struct KeyBoardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } //安装键盘钩子 public void HookStart() { if (hHook == 0) { //实例化一个HookProc对象 KeyBoardProcedure = new HookProc(KeyBoardHookProc); //创建线程钩子 hHook = SetWindowsHookEx(WH_KEYBOARD, KeyBoardProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0); //如果设置线程钩子失败 if (hHook == 0) { HookClear(); } } } //取消钩子 public void HookClear() { bool rsetKeyboard = true; if (hHook != 0) { rsetKeyboard = UnHookWindowsHookEx(hHook); hHook = 0; } if (!rsetKeyboard) { throw new Exception("取消钩子失败!"); } } //对截获的键盘操作的处理 public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct)); if (kbh.vkCode == 91) { MessageBox.Show("按下WIN键"); } } return CallNextHookEx(hHook, nCode, wParam, lParam); } #endregion private void Form1_Load(object sender, EventArgs e) { HookStart();//启动Hook,截获键盘事件 } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { HookClear(); } } }
if (kbh.vkCode == 91 && wParam==0x100) { MessageBox.Show("按下WIN键"); }