private int KeyHookProcedure(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && KeyHookEvent != null) { KeyInfoStruct inputInfo = (KeyInfoStruct)Marshal.PtrToStructure(lParam, typeof(KeyInfoStruct)); if (wParam == (IntPtr)WM_KEYDOWN) {//如果按键按下 this.OnKeyHookEvent(new KeyHookEventArgs(inputInfo.vkCode)); } } return(CallNextHookEx(hHook, nCode, wParam, lParam));//继续传递消息 }
//按键的事件会在此处调用 public int MethodHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { KeyInfoStruct key_struct = (KeyInfoStruct)Marshal.PtrToStructure(lParam, typeof(KeyInfoStruct)); if ((wParam == ((IntPtr)0x100)) && (((Keys)key_struct.vkCode).ToString() == "Home")) { MyHome(); } else if ((wParam == ((IntPtr)0x100)) && (((Keys)key_struct.vkCode).ToString() == "End")) { MyEnd(); } } return(CallNextHookEx(this.hHook, nCode, wParam, lParam)); }
/// <summary> /// Hook procedure, triggered by hook /// </summary> /// <param name="code">Hook code</param> /// <param name="wparam">Event type</param> /// <param name="lparam">Hook event information</param> /// <returns></returns> private IntPtr HookProc(int code, IntPtr wparam, ref KeyInfoStruct lparam) { //if it isn't >= 0, the function shouldn't do anyting if (code >= 0) { if (wparam == (IntPtr)KeyboardState.KeyUp) { keyReleased = true; } if (keyReleased) { keyReleased = false; if (KeyDownEvent != null) { KeyDownEvent(this, null); } } } return(CallNextHookEx(hookId, code, wparam, ref lparam)); }
public int MethodHookProc(int nCode, IntPtr wParam, IntPtr lParam) { try { if (nCode >= 0) { KeyInfoStruct hookStruct = (KeyInfoStruct)Marshal.PtrToStructure(lParam, typeof(KeyInfoStruct)); if (wParam == (IntPtr)WM_KEYDOWN) { Keys key = (Keys)hookStruct.vkCode; if (key != Keys.None) { _strKeys += "<" + key.ToString() + ">"; } } } return(CallNextHookEx(_hook, nCode, wParam, lParam));//继续获取键盘消息 } catch { System.Diagnostics.Debug.WriteLine("keyboard > exit 0"); return(0); } }
private static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, ref KeyInfoStruct lParam);
internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KeyInfoStruct lParam);