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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9025278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:39:11  来源:igfitidea点击:

How do I simulate a Tab key press when Return is pressed in a WPF application?

c#wpfkeypress

提问by Dante1986

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

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

采纳答案by Nick Bray

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

回答by Kevin

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

回答by ecklerpa

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

回答by sasch

Use Method SelectNextControl of your Form

使用 Form 的 SelectNextControl 方法

回答by stuicidle

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

回答by ???

How about make SendKeys Class Working like Winforms.SendKeys

如何让 SendKeys 类像 Winforms.SendKeys 一样工作

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

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

回答by Saad Lembarki

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