public void ShowDragVisual(Control control) { UpdatePosition(); User32.ShowWindow(this.Handle, 8); User32.SetWindowPos( this.Handle, (IntPtr)(-1), 0, 0, 0, 0, 0x0010 | 0x0002 | 0x001); try { _hook = new LowLevelMouseHook(); _hook.Activate(0); } catch (Win32Exception) { if (_hook != null) { _hook.Dispose(); _hook = null; } } if (_hook == null) { _timer.Enabled = true; if (control != null) { control.GiveFeedback += OnControlGiveFeedback; } } else { _hook.MouseMove += (s, e) => { if (InvokeRequired) { BeginInvoke(new Action <Point>(UpdatePosition), e.Location); } else { UpdatePosition(e.Location); } }; _hook.MouseWheel += (s, e) => { if (e.Delta == 0) { return; } var h = User32.WindowFromPoint(new POINT(e.X, e.Y)); if (h == IntPtr.Zero) { return; } User32.SendMessage(h, WM.MOUSEWHEEL, (IntPtr)(e.Delta << 16), (IntPtr)((e.Y << 16) | e.X)); }; } }
public void DisposeWhenNotActivatedTest() { var hook = new LowLevelMouseHook((evnt, data) => { return(LowLevelMouseHook.Result.Transfer); }); Assert.IsFalse(hook.IsActivated); hook.Dispose(); Assert.IsFalse(hook.IsActivated); }
protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); Hide(); _mouseHook?.Dispose(); _overviewHotkey?.Dispose(); _dropShadow?.Hide(); _wmr?.Dispose(); _quickInfoWindow?.Close(); Environment.Exit(0); }
protected override void Dispose(bool disposing) { if (disposing) { if (_hook != null) { _hook.Dispose(); _hook = null; } _timer.Dispose(); } base.Dispose(disposing); }
protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { AppConfig.ConfigChanged -= AppConfig_ConfigChanged; } LowLevelMouseHook?.Dispose(); disposedValue = true; } }
public void Dispose() { if (hook != null) { try { hook.Dispose(); } catch (Win32Exception) {} } RawDevice.UnregisterRawDevices(0x01, 0x02); RawDevice.UnregisterRawDevices(HID_USAGE_PAGE_DIGITIZER, HID_USAGE_DIGITIZER_PEN); RawDevice.RawInput -= RawDevice_RawInput; Application.Exit(); inputProvider.IsRunning = false; }
static void Main(string[] args) { TestFilter(); return; // creates a new instance to capture inputs // also provides IsPressed, WasPressed and GetState methods var inputManager = new InputManager(); // you may not need those when you use InputManager var keyboardHook = new LowLevelKeyboardHook(); var mouseHook = new LowLevelMouseHook(); // subscribe to the events offered by InputManager inputManager.OnKeyboardEvent += InputManager_OnKeyboardEvent; inputManager.OnMouseEvent += InputManager_OnMouseEvent; // same events as above (in case you only need a specific hook and less functionality) keyboardHook.OnKeyboardEvent += KeyboardHook_OnKeyboardEvent; mouseHook.OnMouseEvent += MouseHook_OnMouseEvent; // we need to initialize our classes before they fire events and are completely usable inputManager.Initialize(); keyboardHook.InstallHook(); mouseHook.InstallHook(); // registers an event (callback) which gets fired whenever the key changes it's state // be sure to use this method after the InputManager is initialized inputManager.RegisterEvent(VirtualKeyCode.Lbutton, InputManager_KeyStateChanged); Console.WriteLine("Waiting for up arrow key to exit!"); // This method will block the current thread until the up arrow key changes it's state to Down // There is no performance penalty (spinning loop waiting for this) inputManager.WaitForEvent(VirtualKeyCode.Up, KeyState.Down); // be sure to dispose instances you dont use anymore // not doing so may block windows input and let inputs appear delayed or lagging // these classes try dispose itself when an unhandled exception occurs or the process exits mouseHook.Dispose(); keyboardHook.Dispose(); inputManager.Dispose(); }
/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"> /// <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only /// unmanaged resources. /// </param> protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (keyboardHook != null) { keyboardHook.Dispose(); } if (mouseHook != null) { mouseHook.Dispose(); } mapKeyState = null; singleKeyCallback = null; } disposedValue = true; } }
/// <summary> /// /// </summary> /// <param name="process"></param> /// <param name="cancellationToken"></param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="InvalidOperationException"></exception> /// <returns></returns> public async Task <Rectangle> SelectAsync( IProcess <ICommand> process, CancellationToken cancellationToken = default) { process = process ?? throw new ArgumentNullException(nameof(process)); if (Window == null) { await InitializeAsync(cancellationToken).ConfigureAwait(false); } Window = Window ?? throw new InvalidOperationException("Window is null."); var scaleFactor = await Dispatcher.InvokeAsync( () => Window.GetDpi(), DispatcherPriority.Normal, cancellationToken); var startPoint = new Point(); var endPoint = new Point(); var currentPoint = new Point(); using var exceptions = new ExceptionsBag(); using var hook = new LowLevelMouseHook { GenerateMouseMoveEvents = true, }; hook.ExceptionOccurred += (_, exception) => { // ReSharper disable once AccessToDisposedClosure exceptions.OnOccurred(exception); }; var isInitialized = false; hook.Move += (_, args) => { currentPoint = args.Position; if (isInitialized) { return; } startPoint = currentPoint.ToApp(scaleFactor); endPoint = startPoint; isInitialized = true; }; hook.Start(); using var timer = new Timer(15); timer.Elapsed += (_, _) => { if (!isInitialized) { return; } endPoint = currentPoint.ToApp(scaleFactor); Dispatcher.Invoke(() => { ApplyRectangle( Window, startPoint, endPoint); }); }; timer.Start(); await Dispatcher.InvokeAsync(() => { ApplyRectangle( Window, startPoint, endPoint); Window.Border.Visibility = Visibility.Visible; }, DispatcherPriority.Normal, cancellationToken); await process.WaitAsync(cancellationToken).ConfigureAwait(false); timer.Dispose(); hook.Dispose(); await Dispatcher.InvokeAsync(() => { Window.Border.Visibility = Visibility.Hidden; }, DispatcherPriority.Normal, cancellationToken); return(startPoint .ToRectangle(endPoint) .Normalize() .ToPhysical(scaleFactor)); }