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); } }
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); }
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)); }