在WPF应用程序中按下Return键时如何模拟Tab键?

时间:2021-12-19 00:08:10

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.

在WPF应用程序中,我有一个包含很多字段的窗口。当用户在填充每个字段后使用TAB键时,Windows会理解它会移动到下一个字段。这是非常了解的行为。

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"

现在我想要的是,它使模拟TAB键,实际上RETURN被击中。所以在我的WPF xaml中我添加了暗示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

编辑1 ==>找到一个解决方案

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 :)

这样,焦点移动到下一个它可以找到:)

5 个解决方案

#1


13  

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

你可以在这里看一篇文章: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);
            }
        }

#2


3  

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

#3


1  

I think you should use that to simulate TAB :

我认为你应该用它来模拟TAB:

SendKeys.Send("{TAB}");

Instead of

代替

e.Key = Key.Tab

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

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

#4


0  

Use Method SelectNextControl of your Form

使用表单的SelectNextControl方法

#5


0  

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

SendKeys.Send或SendKeys.SendWait在WPF应用程序中不起作用,所以回答原始问题

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

#1


13  

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

你可以在这里看一篇文章: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);
            }
        }

#2


3  

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

#3


1  

I think you should use that to simulate TAB :

我认为你应该用它来模拟TAB:

SendKeys.Send("{TAB}");

Instead of

代替

e.Key = Key.Tab

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

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

#4


0  

Use Method SelectNextControl of your Form

使用表单的SelectNextControl方法

#5


0  

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

SendKeys.Send或SendKeys.SendWait在WPF应用程序中不起作用,所以回答原始问题

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