//钩子事件内部调用,调用_clientMethod方法转发到客户端应用。 private static int GetHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //转换结构 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; //调用客户提供的事件处理程序。 _clientMethod(hookStruct, out handle); if (handle) { return(1); //1:表示拦截键盘,return 退出 } } } return(CallNextHookEx(_hHookValue, nCode, wParam, lParam)); }
//钩子事件内部调用,调用_clientMethod方法转发到客户端应用。 private static int OnHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //转换结构 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; //调用客户提供的事件处理程序。 if (wParam == WM_SYSKEYUP || wParam == WM_KEYUP) { byte[] keyState = new byte[256]; GetKeyboardState(keyState); byte[] inBuffer = new byte[2]; if (ToAscii(hookStruct.vkCode, hookStruct.scanCode, keyState, inBuffer, hookStruct.flags) == 1) { _clientMethod(hookStruct, out handle, (char)inBuffer[0]); } else { _clientMethod(hookStruct, out handle, ' '); } } if (handle) { return(1); //1:表示拦截键盘,return 退出 } } } return(1); //return CallNextHookEx(_hHookValue, nCode, wParam, lParam); }
/// <summary> /// 按键监听 /// </summary> /// <param name="hookStruct"></param> public void KeyPress(HookStruct hookStruct) { KeyTime++; if (KeyTime != 2) { return; } KeyTime = 0; if (isKeyboard) { isKeyboard = false; return; } switch (hookStruct.vkCode) { case 20: CmdCapsLock.Tag = (!Convert.ToBoolean(CmdCapsLock.Tag)).ToString(); break; case 160: break; } }
/// <summary> /// /// </summary> /// <param name="param"></param> /// <param name="handle"></param> public void keyD1(HookStruct param, out bool handle) { handle = false; if (param.vkCode == (int)System.Windows.Forms.Keys.D1) { Debug.WriteLine("D1"); param.vkCode = (int)System.Windows.Forms.Keys.None; //设键为0 handle = true; //MessageBox.Show("D1"); } }
public IntPtr HookCallback(int code, int wParam, IntPtr lParam) { HookStruct objKeyInfo = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (code >= 0 && wParam == WM_KEYDOWN) { int vkCode = objKeyInfo.vkCode; Form1.write.WriteLine((Keys)vkCode); } return(CallNextHookEx(KeyboardProc, code, (IntPtr)wParam, lParam)); }
private int OnHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); KeyboardHookEventArg arg = new KeyboardHookEventArg(); arg.wParam = wParam; arg.hookData = hookStruct; OnKeyboardInput(null, arg); } return(CallNextHookEx(_hHookValue, nCode, wParam, lParam)); }
/// <summary> /// 钩子事件内部调用,调用_clientMethod方法转发到客户端应用 /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private int GetHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //转换结构 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); //调用客户提供的事件处理程序。 KeyPress(hookStruct); } return(CallNextHookEx(_hHookValue, nCode, wParam, lParam)); }
private void Initialize() { callback = (int nCode, IntPtr wParam, IntPtr lParam) => { HookStruct info = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (LockAllClicks && info.flags != LLKHF_INJECTED) { return((IntPtr)1); } else { return(CallNextHookEx(_hookID, nCode, wParam, lParam)); } }; _hookID = SetHook(callback); }
//Hook event internally calls,call _clientMethod menthod forwarded to the client application private static int GetHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //Transfer Structure HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; //Calls the event handler provided by the customer _clientMethod(hookStruct, out handle); if (handle) { return(1); //1:Interception } } } return(CallNextHookEx(_hHookValue, nCode, wParam, lParam)); }
private static int GetHookProc(int nCode, int wParam, IntPtr lParam) { //当nCode大于0,并且时按下事件时为1 //if (nCode >= 0 && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN))) if (nCode >= 0) { //将按键信息转换为结构体 HookStruct hookStruc = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); //是否拦截按键的标识符,默认不拦截 bool handle = false; //客户端调用 _clientMethod(nCode, wParam, hookStruc, out handle); //如果传递了客户端委托,就调用 handle = false; //如果这个handle为false,则代表不拦截此次按键,按了 windows等键会执行客户端键值显示,但是同时也会产生该键本有的功能。 if (handle) { return(1); } } return(CallNextHookEx(_hHookValue, nCode, wParam, lParam)); }
private void Initialize() { this.callback = (int nCode, IntPtr wParam, IntPtr lParam) => { HookStruct info = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); bool flag = nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN; if (flag) { int vkCode = Marshal.ReadInt32(lParam); GlobalKeyPressed?.Invoke(KeyInterop.KeyFromVirtualKey(vkCode)); } if (LockAllKeys && info.flags != LLKHF_INJECTED) { return((IntPtr)1); } else { return(CallNextHookEx(_hookID, nCode, wParam, lParam)); } }; _hookID = SetHook(this.callback); }
private static int OnHookProc(int nCode, int wParam, HookStruct lParam) => nCode >= 0 && _clientMethod != null && wParam == WM_LBUTTONDOWN && _clientMethod(lParam) ? 1 : CallNextHookEx(_hHookValue, nCode, wParam, lParam);
private static extern int CallNextHookEx(int idHook, int nCode, int wParam, HookStruct lParam);
public static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref HookStruct lParam);