/// <summary> /// Attach the given handler to intercept the given message types /// </summary> /// <param name="messageType"></param> /// <param name="handler"></param> /// <returns></returns> public static IntPtr SetHook(HookID messageType, HookProcess handler) { using (Process currentProcess = Process.GetCurrentProcess()) { using (ProcessModule currentModule = currentProcess.MainModule) { var hook = SetWindowsHookEx(Convert.ToInt32(messageType), handler, GetModuleHandle(currentModule.ModuleName), 0); lock (CurrentHooksLock) { CurrentlySetHooks.Add(hook, handler); } return(hook); } } }
private void RegisterProc(HookID hId, HookProc proc) { if (!this._currentHooks.ContainsKey(hId)) { // Hooking the delegate to the system. // Hooks might be removed by timeout. Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644986(v=vs.85).aspx var hHook = SetWindowsHookEx((int)hId, proc, IntPtr.Zero, 0); // TODO: Module might be needed... // Storing the callback. this._currentHooks.Add(hId, new SysHook() { HookHandle = hHook, Procedure = proc, Callbacks = new Dictionary <uint, KeyValuePair <HookType, Action <SystemInputEvent> > >() }); } }