/// <summary> /// Receives notificaton from a logical key that it has been pressed. The keyboard control /// uses this notificatoin to monitor and reset modifier keys if necessary. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void LogicalKeyPressed(object sender, LogicalKeyEventArgs e) { if (sender is ModifierKeyBase modifierKey) { RaiseModifierChangedEvent((VirtualKeyCode)modifierKey.KeyCode, modifierKey.IsInEffect); } else // not a modifier key { ResetInstantaneousModifierKeys(); } modifierKeys.OfType <InstantaneousModifierKey>().ToList().ForEach(x => x.SynchroniseKeyState()); }
internal override void ScreenKeyPress() { ModifiedKeyStrokes(modifierKeys); List <WindowsInput.Native.VirtualKeyCode> pressedKeys = new List <WindowsInput.Native.VirtualKeyCode> { KeyCode }; pressedKeys.AddRange(modifierKeys); LogicalKeyEventArgs args = new LogicalKeyEventArgs(pressedKeys); OnKeyPress(args); }
/// <summary> /// Raises the KeyPressed event. /// </summary> /// <param name="args"></param> protected virtual void OnKeyPress(LogicalKeyEventArgs args) { // Get the event handler containing the registered listeners. EventHandler <LogicalKeyEventArgs> handler = KeyPressed; // If there are no listeners, if (handler == null) { // Nothing to do. return; } // Extract the list of registered listeners. Delegate[] listeners = handler.GetInvocationList(); EventHandler <LogicalKeyEventArgs> stateChangeListener; // For each registered listener in the list, foreach (Delegate listener in listeners) { try { // Get the state change listener. stateChangeListener = listener as EventHandler <LogicalKeyEventArgs>; // Broadcast the event with the arguments provided. stateChangeListener?.Invoke(this, args); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { System.Diagnostics.Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "KeyPressed listener threw exception: {0}", ex)); // Don't rethrow the exception . } } }