public static void UnregisterHook(KeyHook keyHook) { if (hook == null) { return; } hook.KeyHooks.Remove(keyHook); }
public static void RegisterHook(KeyHook keyHook) { if (hook == null || hook.Handle == IntPtr.Zero) { InstallHook(keyHook); return; } hook.KeyHooks.Add(keyHook); }
private static IntPtr InstallHook(KeyHook keyHook) { using (var curProc = Process.GetCurrentProcess()) using (var curModule = curProc.MainModule) { var handle = HookAPI.SetWindowsHookEx(HookType.WH_KEYBOARD_LL, hookProc, HookAPI.GetModuleHandle(curModule.ModuleName), 0); if (handle == IntPtr.Zero) { throw new HookException("Failed to install hook"); } hook = new HookContainer { Handle = handle, KeyHooks = new List <KeyHook> { keyHook } }; return(handle); } }