C# SetWindowsHookEx
[DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId); /// <summary> /// 安装钩子 /// </summary> /// <param name="idHook">钩子类型 13键盘和14鼠标,用来对底层输入事件监视</param> /// <param name="callback">函数指针</param> /// <param name="hInstance">包含SetWindowsHookEx函数的模块地址,user32.dll入口</param> /// <param name="threadId">0表示系统钩子,</param> /// <param name="str"></param> /// <returns></returns> public IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId, string str = "占位符") { return SetWindowsHookEx(idHook, callback, hInstance, threadId); }
public event KeyEventHandler KeyDown; private void button7_Click(object sender, EventArgs e) { this.KeyDown += new KeyEventHandler(gkh_KeyDown); //绑定事件 Keys key = (Keys)1; KeyEventArgs kea = new KeyEventArgs(key); KeyDown(this, kea); } private void gkh_KeyDown(object sender, KeyEventArgs e) { MessageBox.Show("000111"); }