public static IntPtr SetHook(LowLevelKeyboardProcedure proc) { using (var curProcess = Process.GetCurrentProcess()) using (var curModule = curProcess.MainModule) { return(SetWindowsHookEx(WhKeyboardLl, proc, GetModuleHandle(curModule.ModuleName), 0)); } }
public static IntPtr Hook(LowLevelKeyboardProcedure procedure) { _CallbackRef = procedure; using (Process currentProcess = Process.GetCurrentProcess()) { using (ProcessModule currentModule = currentProcess.MainModule) { return(SetWindowsHookEx(KeyboardLowLevel, procedure, GetModuleHandle(currentModule.ModuleName), 0)); } } }
/// <summary> /// Registers a low-level global keyboard system hook. /// </summary> /// public static HookHandle SetWindowHook(LowLevelKeyboardProcedure callback) { IntPtr hHook; NativeMethods.LowLevelHookProc lpfn; using (Process process = Process.GetCurrentProcess()) using (ProcessModule module = process.MainModule) { IntPtr hModule = NativeMethods.GetModuleHandle(module.ModuleName); lpfn = new NativeMethods.LowLevelHookProc((nCode, wParam, lParam) => { // From // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985(v=vs.85).aspx // // wParam contains the identifier of the keyboard message. // lParam contains a pointer to a KBDLLHOOKSTRUCT structure. // // The wParam can be can be one of the following messages: WM_KEYDOWN, // WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP. if (nCode < 0) { return(NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)); } KeyboardLowLevelHookStruct keyboardInfo = (KeyboardLowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardLowLevelHookStruct)); callback((LowLevelKeyboardMessage)(wParam.ToInt32()), keyboardInfo); return(NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)); }); hHook = NativeMethods.SetWindowsHookEx( NativeMethods.HookType.WH_KEYBOARD_LL, lpfn, hModule, 0); } return(new HookHandle(hHook, lpfn)); }
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcedure lpfn, IntPtr hMod, uint dwThreadId);
/// <summary> /// Registers a low-level global keyboard system hook. /// </summary> /// public static HookHandle SetWindowHook(LowLevelKeyboardProcedure callback) { IntPtr hHook; NativeMethods.LowLevelHookProc lpfn; using (Process process = Process.GetCurrentProcess()) using (ProcessModule module = process.MainModule) { IntPtr hModule = NativeMethods.GetModuleHandle(module.ModuleName); lpfn = new NativeMethods.LowLevelHookProc((nCode, wParam, lParam) => { // From // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985(v=vs.85).aspx // // wParam contains the identifier of the keyboard message. // lParam contains a pointer to a KBDLLHOOKSTRUCT structure. // // The wParam can be can be one of the following messages: WM_KEYDOWN, // WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP. if (nCode < 0) return NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam); KeyboardLowLevelHookStruct keyboardInfo = (KeyboardLowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardLowLevelHookStruct)); callback((LowLevelKeyboardMessage)(wParam.ToInt32()), keyboardInfo); return NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam); }); hHook = NativeMethods.SetWindowsHookEx( NativeMethods.HookType.WH_KEYBOARD_LL, lpfn, hModule, 0); } return new HookHandle(hHook, lpfn); }
public Keyboard() { _hookedLowLevelKeyboardProc = LowLevelKeyboardProc; _hookId = SetHook(_hookedLowLevelKeyboardProc); //_hookedKeyboardCallbackAsync = KeyboardCallback; }