views:

1171

answers:

2

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?

A: 

I simulate what you say fine using the following is this not what you mean?

public Window1()
{
    InitializeComponent();


    Loaded += new RoutedEventHandler(Window1_Loaded);
}

void Window1_Loaded(object sender, RoutedEventArgs e)
{
    WebBrowser1_PreviewKeyDown(this, new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 1, Key.LeftShift));
    WebBrowser1_PreviewKeyDown(this, new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 1, Key.Tab));
}

private void WebBrowser1_PreviewKeyDown(object sender, KeyEventArgs e)
{
    System.Diagnostics.Debug.WriteLine(e.Key);
}

OUTPUT:

LeftShift
Tab

Andrew

REA_ANDREW
Thanks, but I don't think this is it. What I'm looking for is a key-stroke combination of Shift-Tab at the same time.
mmccurrey
hmm I see. It is weird because using Key.LeftShift | Key.Tab results in a CtrlLeft for some weird but probably good reason. I will have a little more investigation when I can
REA_ANDREW
@REA_ANDREW: You see this happening because `Keys` are not flags that can be combined, it's an enumeration where `Key.LeftShift = 116` and `Key.Tab = 3`. The result is 119 which maps to `Key.RightCtrl = 119` (Press F12 in Visual Studio when the cursor is on `Key` to open the definition).
0xA3
+1  A: 

I've created a little library that might probably help you out with mocking the Shift key:

http://wpfsendkeys.codeplex.com/

http://blogs.msdn.com/b/kirillosenkov/archive/2010/07/09/wpf-sendkeys-or-mocking-the-keyboard-in-wpf.aspx

Kirill Osenkov