Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Send Keys Redux

So, I'm using a third-part wpf grid control that is hard-coded to only accept certain keystrokes to perform short-cut reactions and one of those is Shift-Tab. However, my user-base is used to hitting up arrow and down arrow and telling them 'no' isn't an option right now. So my only option I think is to intercept the preview key down and send a different key stroke combination.

Now, I am using the following code that I found on here to send a Tab when the user presses the Down arrow:

if (e.Key == Key.Down)
{
    e.Handled = true;
    KeyEventArgs eInsertBack = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab);
    eInsertBack.RoutedEvent = UIElement.KeyDownEvent;
    InputManager.Current.ProcessInput(eInsertBack);
}

However, this method is limited in that you don't seem to be able to simulate a press of the Shift Button? WPF seems to look at the Keyboard.Modifiers to be able to 'read' a Shift or Ctrl, but there doesn't seem to be any facility to Set the Keyboard.Modifiers programatically. Any help out there?

like image 423
mmccurrey Avatar asked Aug 11 '09 21:08

mmccurrey


2 Answers

try this

System.Windows.Forms.SendKeys.SendWait("{Tab}");

In WPF Application, SendKeys.Send not working, But SendWait is working fine.

like image 79
Chitta Avatar answered Sep 29 '22 14:09

Chitta


Create a MockKeyboardDevice like this (kudos to Jared Parsons):

https://github.com/VsVim/VsVim/blob/master/Src/VimTestUtils/Mock/MockKeyboardDevice.cs

Usage:

var modKey = ModifierKeys.Shift;
var device = new MockKeyboardDevice(InputManager.Current)
    {
        ModifierKeysImpl = modKey
    };
var keyEventArgs = device.CreateKeyEventArgs(Key.Tab, modKey);
...

A usage example:

https://github.com/jaredpar/VsVim/blob/master/Test/VimWpfTest/VimKeyProcessorTest.cs

like image 24
Dunc Avatar answered Sep 29 '22 12:09

Dunc