private void Window_Loaded(object sender, RoutedEventArgs e) { if (this.dispatcher == null) { this.dispatcher = this.Dispatcher; this.dispatcherHooks = this.dispatcher.Hooks; } WindowInteropHelper wndHelper = new WindowInteropHelper(this); IntPtr wpfHwnd = wndHelper.Handle; _keyboardDriver = new RawKeyboard(wpfHwnd); _keyboardDriver.EnumerateDevices(); _devNotifyHandle = RegisterForDeviceNotifications(wpfHwnd); System.Windows.Interop.ComponentDispatcher.ThreadFilterMessage += new System.Windows.Interop.ThreadMessageEventHandler(ComponentDispatcher_ThreadFilterMessage); System.Windows.Interop.ComponentDispatcher.ThreadPreprocessMessage += new System.Windows.Interop.ThreadMessageEventHandler(ComponentDispatcher_ThreadPreprocessMessage); this.KeyPressed += new RawKeyboard.DeviceEventHandler(OnKeyPressed); }
public DispatcherWorkerThreadDetails(Thread thread) { this.Thread = thread; Dispatcher dispatcher; // Get the Dispatcher instance for the thread do { dispatcher = Dispatcher.FromThread(thread); // The dispatcher might not have been spun up yet so, if not, sleep and try again until it is if (dispatcher == null) { Thread.Sleep(100); } } while (dispatcher == null); // Hook the events that tell us when the Dispatcher is actually doing something DispatcherHooks dispatcherHooks = dispatcher.Hooks; dispatcherHooks.DispatcherInactive += (s, a) => this.HasWork = false; dispatcherHooks.OperationPosted += (s, a) => this.HasWork = true; }