public Guid RegisterKeyboardHook(KeyboardHookCallback callback) { var hookId = default(Guid); var hookReadyEvent = new AutoResetEvent(false); var hookThread = new Thread(() => { var hook = new KeyboardHook(callback); var sleepEvent = new AutoResetEvent(false); hook.Attach(); hookId = hook.Id; KeyboardHooks[hookId] = hook; hookReadyEvent.Set(); while (true) { sleepEvent.WaitOne(); } }); hookThread.SetApartmentState(ApartmentState.STA); hookThread.IsBackground = true; hookThread.Start(); hookReadyEvent.WaitOne(); return(hookId); }
public void RegisterKeyboardHook(IKeyboardInterceptor interceptor) { var hookReadyEvent = new AutoResetEvent(false); var hookThread = new Thread(() => { var hook = new KeyboardHook(interceptor); var sleepEvent = new AutoResetEvent(false); hook.Attach(); KeyboardHooks[hook.Handle] = hook; hookReadyEvent.Set(); while (true) { sleepEvent.WaitOne(); } }); hookThread.SetApartmentState(ApartmentState.STA); hookThread.IsBackground = true; hookThread.Start(); hookReadyEvent.WaitOne(); }