21

In a WPF application, i have a window that has a lot of fields. When the user uses the TAB key after filling each field, windows understands that it moves on to the next. This is pretty know behavior.

Now what I want to to, is make it simulate the TAB key, when in fact the RETURN gets hit. So in my WPF xaml I added imply KeyDown="userPressEnter"

And in the code behind it:

private void userPressEnter(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Return)
  {
    e.Key = Key.Tab // THIS IS NOT WORKING
  }
}

Now, obviously this is not working. But what I don't know is, how DO I make this work?


EDIT 1 ==> FOUND A SOLUTION

I found something that helped me out =)

private void userPressEnter(object sender, KeyEventArgs e)
{
 if (e.Key == Key.Return)
 {
   TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
   MoveFocus(request);
 }
}

This way the Focus moves on the the next it can find :)

BenR
  • 11,296
  • 3
  • 28
  • 47
Dante1986
  • 58,291
  • 13
  • 39
  • 54
  • By simulate tab do you mean to move the cursor to the next field in the window? – Tudor Jan 26 '12 at 21:03
  • Yea, move it to the next handler, so that can be a field or an button. So simulate the same behavior as if the user whould have hit TAB. That's why i simply try to give the system the TAB input, when in fact RETURN is pressed. – Dante1986 Jan 26 '12 at 21:04
  • Problem of your solution: You need to add it to every single control, otherwise MoveFocus will not select the correct next field. – Sam Aug 30 '12 at 09:52

7 Answers7

18

You can look at a post here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/c85892ca-08e3-40ca-ae9f-23396df6f3bd

Here's an example:

private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                request.Wrapped = true;
                ((TextBox)sender).MoveFocus(request);
            }
        }
Nick Bray
  • 1,953
  • 12
  • 18
4
    protected override bool ProcessDialogKey(Keys keyData)
    {
        System.Diagnostics.Debug.WriteLine(keyData.ToString());

        switch (keyData)
        {
            case Keys.Enter:
                SendKeys.Send("{TAB}");
                break;
        }
        base.ProcessDialogKey(keyData);
        return false;
    }
ecklerpa
  • 159
  • 1
  • 7
  • 1
    This is a winforms solution. SendKeys is in the System.Windows.Forms dll and not in the System.Windows dll – Mafii Jul 18 '16 at 11:30
4

How about make SendKeys Class Working like Winforms.SendKeys

https://michlg.wordpress.com/2013/02/05/wpf-send-keys/

public static class SendKeys
{
    public static void Send(Key key)
    {
        if (Keyboard.PrimaryDevice != null) {
            if (Keyboard.PrimaryDevice.ActiveSource != null) {
                var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key) { RoutedEvent = Keyboard.KeyDownEvent };
                InputManager.Current.ProcessInput(e1);
            }
        }
    }
}
최승훈
  • 41
  • 1
  • This should be the accepted answer, as none of the other answers actually trigger another tab press event. – MikeLimaSierra Mar 10 '21 at 14:26
  • Much better solution than answer - FocusNavigationDirection.Next won't get into next control from editable ComboBox. – Lucy82 Jun 05 '23 at 11:26
2

I think you should use that to simulate TAB :

SendKeys.Send("{TAB}");

Instead of

e.Key = Key.Tab

Sources : http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

Kevin
  • 1,019
  • 10
  • 16
  • 1
    I know this method works for winforms apps, but does it also work for WPF? – Tudor Jan 26 '12 at 21:09
  • 2
    You're right it doesn't... I've found this instead but i'm sure there is a better solution http://inputsimulator.codeplex.com/ – Kevin Jan 26 '12 at 21:12
1

SendKeys.Send or SendKeys.SendWait will not work in a WPF application, so to answer the original question

if (e.Key == Key.Return)
{    
    KeyEventArgs tabPressEventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab) { RoutedEvent = Keyboard.KeyDownEvent };
    InputManager.Current.ProcessInput(tabPressEventArgs); 
}
stuicidle
  • 297
  • 1
  • 8
0

Use Method SelectNextControl of your Form

sasch
  • 1
0

I think the best solution is:

var ue = e.OriginalSource as FrameworkElement;

if (e.Key == Key.Return)
{
    e.Handled = true;
    ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
Milo
  • 3,365
  • 9
  • 30
  • 44
Saad Lembarki
  • 482
  • 5
  • 6