private void PressKey(IntellisenseWindow window, Key key) { var keyDown = keyboard.CreateKeyEventArgs(key, Keyboard.KeyDownEvent); window.RaiseEvent(keyDown); var keyUp = keyboard.CreateKeyEventArgs(key, Keyboard.KeyUpEvent); window.RaiseEvent(keyUp); }
/// <summary> /// Sends key strokes to the provided viewmodel. /// If the input would cause the intellisense window to /// trigger, the intellisense callback is invoked. /// </summary> /// <param name="keyStrokes">A FormattableString (not a string!) that contains keystrokes</param> /// <param name="vm">window view model</param> /// <param name="intellisenseCallback"></param> /// <returns></returns> private async Task TypeInput(FormattableString keyStrokes, WindowViewModel vm, TriggerIntellisense intellisenseCallback = null) { var device = new MockKeyboardDevice(InputManager.Current); ModifierKeys modifier = ModifierKeys.None; foreach (var stroke in ConvertToKeys(keyStrokes)) { var currentLine = vm.Entries[vm.FocusIndex]; // set up document state; in the real app this is done by databinding if (currentLine.Document is null) { var editor = new TextEditor { Document = new TextDocument() }; currentLine.Document ??= editor.Document; currentLine.SetEditor(editor); currentLine.TriggerIntellisense ??= intellisenseCallback ?? ((items) => { }); } // convert to input to the appropriate key press Key key; switch (stroke) { case char ch: // type the character into the editor and set up the key event currentLine.Document.Text += ch; currentLine.CaretOffset = currentLine.Document.Text.Length; key = CharToKey.ContainsKey(ch) ? CharToKey[ch] : (Key)keyConverter.ConvertFromString(ch.ToString()); break; case Key k: key = k; break; case ModifierKeys mod: modifier |= mod; continue; default: throw new InvalidOperationException("Unhandled type: " + stroke.GetType()); } device.ModifierKeysImpl = modifier; var keyDown = device.CreateKeyEventArgs(key, Keyboard.KeyDownEvent); await viewModelService.HandleKeyDown(vm, currentLine, keyDown); var keyUp = device.CreateKeyEventArgs(key, Keyboard.KeyUpEvent); await viewModelService.HandleKeyUp(vm, currentLine, keyUp); modifier = ModifierKeys.None; } }
private void VerifyCore(Key key, ModifierKeys modKeys, bool shouldHandle) { var args = _device.CreateKeyEventArgs(key, modKeys); _vsProcessor.KeyDown(args); Assert.Equal(shouldHandle, args.Handled); }
protected static KeyEventArgs CreateKeyEventArgs( Key key, ModifierKeys modKeys = ModifierKeys.None) { var device = new MockKeyboardDevice(InputManager.Current) { ModifierKeysImpl = modKeys }; return(device.CreateKeyEventArgs(key, modKeys)); }