/// <summary> /// Run the KeyInput directly in the Wpf control /// </summary> private void RunInWpf(KeyInput keyInput) { Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(typeof(UIPermissionAttribute)); var presentationSource = _factory.Create <PresentationSource>(); // Normalize upper case letters here to lower and add the shift key modifier if // necessary if (Char.IsUpper(keyInput.Char)) { var lowerKeyInput = KeyInputUtil.CharToKeyInput(Char.ToLower(keyInput.Char)); keyInput = KeyInputUtil.ChangeKeyModifiers(lowerKeyInput, keyInput.KeyModifiers | KeyModifiers.Shift); } Key key; if (!KeyUtil.TryConvertToKeyOnly(keyInput.Key, out key)) { throw new Exception("Couldn't get the WPF key for the given KeyInput"); } // Update the KeyModifiers while this is being processed by our key processor try { _defaultKeyboardDevice.DownKeyModifiers = keyInput.KeyModifiers; // First raise the KeyDown event var keyDownEventArgs = new KeyEventArgs( _defaultKeyboardDevice, presentationSource.Object, 0, key); keyDownEventArgs.RoutedEvent = UIElement.KeyDownEvent; _defaultInputController.HandleKeyDown(this, keyDownEventArgs); // If the event is handled then return if (keyDownEventArgs.Handled) { return; } // Now raise the TextInput event var textInputEventArgs = new TextCompositionEventArgs( _defaultKeyboardDevice, new TextComposition(InputManager.Current, _wpfTextView.VisualElement, keyInput.Char.ToString())); textInputEventArgs.RoutedEvent = UIElement.TextInputEvent; _defaultInputController.HandleTextInput(this, textInputEventArgs); var keyUpEventArgs = new KeyEventArgs( _defaultKeyboardDevice, presentationSource.Object, 0, key); keyUpEventArgs.RoutedEvent = UIElement.KeyUpEvent; _defaultInputController.HandleKeyUp(this, keyUpEventArgs); } finally { _defaultKeyboardDevice.DownKeyModifiers = KeyModifiers.None; } }
/// <summary> /// Run the KeyInput directly in the Wpf control /// </summary> private void Run(string text, Key key, ModifierKeys modifierKeys) { // First raise the KeyDown event var keyDownEventArgs = new KeyEventArgs( _defaultKeyboardDevice, _presentationSource.Object, 0, key); keyDownEventArgs.RoutedEvent = UIElement.KeyDownEvent; _defaultInputController.HandleKeyDown(this, keyDownEventArgs); // If the event is handled then return if (keyDownEventArgs.Handled) { return; } // Now raise the TextInput event var textInputEventArgs = new TextCompositionEventArgs( _defaultKeyboardDevice, CreateTextComposition(text)); textInputEventArgs.RoutedEvent = UIElement.TextInputEvent; _defaultInputController.HandleTextInput(this, textInputEventArgs); var keyUpEventArgs = new KeyEventArgs( _defaultKeyboardDevice, _presentationSource.Object, 0, key); keyUpEventArgs.RoutedEvent = UIElement.KeyUpEvent; _defaultInputController.HandleKeyUp(this, keyUpEventArgs); }
private bool RunDown(KeyInput keyInput, Key key, ModifierKeys modifierKeys) { // First let the preprocess step have a chance to intercept the key if (PreProcess(KeyDirection.Down, keyInput, key, modifierKeys)) { return(true); } // Next raise the preview event // First raise the PreviewKeyDown event var previewKeyDownEventArgs = CreateKeyEventArgs(key, UIElement.PreviewKeyDownEvent); _defaultInputController.HandlePreviewKeyDown(this, previewKeyDownEventArgs); if (previewKeyDownEventArgs.Handled) { return(true); } // If the preview event wasn't handled then move to the down event var keyDownEventArgs = CreateKeyEventArgs(key, UIElement.KeyDownEvent); _defaultInputController.HandleKeyDown(this, keyDownEventArgs); return(keyDownEventArgs.Handled); }