public VsKeyInfo WaitKey() { try { // raise the StartWaitingKey event on main thread RaiseEventSafe(StartWaitingKey); // set/reset the cancellation token _cancelWaitKeySource = new CancellationTokenSource(); _isExecutingReadKey = true; // blocking call VsKeyInfo key = _keyBuffer.Take(_cancelWaitKeySource.Token); return(key); } catch (OperationCanceledException) { return(null); } finally { _isExecutingReadKey = false; } }
public void SimplePostKeyWaitKey() { var privateWpfConsole = new Mock <IPrivateWpfConsole>(); var dispatcher = new ConsoleDispatcher(privateWpfConsole.Object); var postedKey = VsKeyInfo.Create(Key.Z, 'z', 0); dispatcher.PostKey(postedKey); // test key available Assert.True(dispatcher.IsKeyAvailable); // queue a cancel operation to prevent test getting "stuck" // should the following WaitKey call fail bool cancelWasQueued = InteractiveHelper.TryQueueCancelWaitKey(dispatcher, timeout: TimeSpan.FromSeconds(5)); Assert.True(cancelWasQueued); // blocking VsKeyInfo keyInfo = dispatcher.WaitKey(); Assert.Equal(keyInfo, postedKey); // queue should be empty Assert.False(dispatcher.IsKeyAvailable); }
private void ExecuteReadKey() { var cki = Console.ReadKey(intercept: true); // catch current modifiers as early as possible var capsLockToggled = Keyboard.IsKeyToggled(Key.CapsLock); var numLockToggled = Keyboard.IsKeyToggled(Key.NumLock); // convert from char to virtual key, using current thread's input locale var keyScan = NativeMethods.VkKeyScanEx(cki.KeyChar, _pKeybLayout); // virtual key is in LSB, shiftstate in MSB. var virtualKey = (byte)(keyScan & 0x00ff); // convert from virtual key to wpf key. var key = KeyInterop.KeyFromVirtualKey(virtualKey); // create nugetconsole.vskeyinfo to marshal info to var keyInfo = VsKeyInfo.Create( key: key, keyChar: cki.KeyChar, virtualKey: virtualKey, keyStates: KeyStates.Down, shiftPressed: cki.Modifiers.HasFlag(ConsoleModifiers.Shift), altPressed: cki.Modifiers.HasFlag(ConsoleModifiers.Alt), controlPressed: cki.Modifiers.HasFlag(ConsoleModifiers.Control), capsLockToggled: capsLockToggled, numLockToggled: numLockToggled); _keyBuffer.Add(keyInfo); }
internal static void PostKeys(ConsoleDispatcher dispatcher, string line, bool appendCarriageReturn = false, TimeSpan timeout = default(TimeSpan)) { IntPtr pKeybLayout = NativeMethods.GetKeyboardLayout(0); foreach (char keyChar in line) { short keyScan = NativeMethods.VkKeyScanEx(keyChar, pKeybLayout); byte virtualKey = (byte)(keyScan & 0x00ff); VsKeyInfo keyInfo = VsKeyInfo.Create( KeyInterop.KeyFromVirtualKey(virtualKey), keyChar, virtualKey); dispatcher.PostKey(keyInfo); } if (appendCarriageReturn) { dispatcher.PostKey(VsKeyInfo.Enter); } // queue a cancel operation to prevent test getting "stuck" if (timeout != TimeSpan.Zero) { var cancelWasQueued = TryQueueCancelWaitKey(dispatcher, timeout); Assert.True(cancelWasQueued); } }
public void PostKey(VsKeyInfo key) { if (key == null) { throw new ArgumentNullException("key"); } _keyBuffer.Add(key); }
private VsKeyInfo GetVsKeyInfo(IntPtr pvaIn, VSConstants.VSStd2KCmdID commandID) { // catch current modifiers as early as possible bool capsLockToggled = Keyboard.IsKeyToggled(Key.CapsLock); bool numLockToggled = Keyboard.IsKeyToggled(Key.NumLock); char keyChar; if ((commandID == VSConstants.VSStd2KCmdID.RETURN) && pvaIn == IntPtr.Zero) { // <enter> pressed keyChar = Environment.NewLine[0]; // [CR]LF } else if ((commandID == VSConstants.VSStd2KCmdID.BACKSPACE) && pvaIn == IntPtr.Zero) { keyChar = '\b'; // backspace control character } else { Debug.Assert(pvaIn != IntPtr.Zero, "pvaIn != IntPtr.Zero"); // 1) deref pointer to char keyChar = (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn); } // 2) convert from char to virtual key, using current thread's input locale short keyScan = NativeMethods.VkKeyScanEx(keyChar, _pKeybLayout.Value); // 3) virtual key is in LSB, shiftstate in MSB. byte virtualKey = (byte)(keyScan & 0x00ff); keyScan = (short)(keyScan >> 8); byte shiftState = (byte)(keyScan & 0x00ff); // 4) convert from virtual key to wpf key. Key key = KeyInterop.KeyFromVirtualKey(virtualKey); // 5) create nugetconsole.vskeyinfo to marshal info to var keyInfo = VsKeyInfo.Create( key, keyChar, virtualKey, keyStates: KeyStates.Down, capsLockToggled: capsLockToggled, numLockToggled: numLockToggled, shiftPressed: ((shiftState & 1) == 1), controlPressed: ((shiftState & 2) == 4), altPressed: ((shiftState & 4) == 2)); return(keyInfo); }
/// <summary> /// This API reads a pressed, released, or pressed and released keystroke /// from the keyboard device, blocking processing until a keystroke is /// typed that matches the specified keystroke options. /// </summary> /// <param name="options">Options, such as IncludeKeyDown, used when reading the keyboard.</param> /// <returns>KeyInfo of the key pressed</returns> public override KeyInfo ReadKey(ReadKeyOptions options) { VsKeyInfo keyInfo = _debuggingService.CallbackService.VsReadKey(); if (keyInfo == null) { // abort current pipeline throw new PipelineStoppedException(); } ControlKeyStates states = default(ControlKeyStates); states |= (keyInfo.CapsLockToggled ? ControlKeyStates.CapsLockOn : 0); states |= (keyInfo.NumLockToggled ? ControlKeyStates.NumLockOn : 0); states |= (keyInfo.ShiftPressed ? ControlKeyStates.ShiftPressed : 0); states |= (keyInfo.AltPressed ? ControlKeyStates.LeftAltPressed : 0); // assume LEFT alt states |= (keyInfo.ControlPressed ? ControlKeyStates.LeftCtrlPressed : 0); // assume LEFT ctrl return(new KeyInfo(keyInfo.VirtualKey, keyInfo.KeyChar, states, keyDown: (keyInfo.KeyStates == KeyStates.Down))); }
public override KeyInfo ReadKey(ReadKeyOptions options) { // NOTE: readkey options are ignored as they are not really usable or applicable in PM console. VsKeyInfo keyInfo = Console.Dispatcher.WaitKey(); if (keyInfo == null) { // abort current pipeline (ESC pressed) throw new PipelineStoppedException(); } ControlKeyStates states = default(ControlKeyStates); states |= (keyInfo.CapsLockToggled ? ControlKeyStates.CapsLockOn : 0); states |= (keyInfo.NumLockToggled ? ControlKeyStates.NumLockOn : 0); states |= (keyInfo.ShiftPressed ? ControlKeyStates.ShiftPressed : 0); states |= (keyInfo.AltPressed ? ControlKeyStates.LeftAltPressed : 0); // assume LEFT alt states |= (keyInfo.ControlPressed ? ControlKeyStates.LeftCtrlPressed : 0); // assume LEFT ctrl return(new KeyInfo(keyInfo.VirtualKey, keyInfo.KeyChar, states, keyDown: (keyInfo.KeyStates == KeyStates.Down))); }
public void HostUserInterfaceReadkey() { Mock <NuGetRawUserInterface> mockRawUI; Mock <NuGetHostUserInterface> mockUI; ConsoleDispatcher dispatcher; InteractiveHelper.InitializeConsole(out mockRawUI, out mockUI, out dispatcher); var postedKey = VsKeyInfo.Create(Key.Z, 'z', 90); dispatcher.PostKey(postedKey); // queue a cancel operation to prevent test getting "stuck" // should the following ReadKey call fail var cancelWasQueued = InteractiveHelper.TryQueueCancelWaitKey(dispatcher, TimeSpan.FromSeconds(5)); Assert.True(cancelWasQueued); KeyInfo keyInfo = mockRawUI.Object.ReadKey(); Assert.Equal(keyInfo.Character, 'z'); Assert.Equal(keyInfo.VirtualKeyCode, 90); Assert.Equal(keyInfo.ControlKeyState, default(ControlKeyStates)); }