【问题标题】:How do I simulate a Tab key press when Return is pressed in a WPF application?在 WPF 应用程序中按下 Return 时如何模拟 Tab 键按下?
【发布时间】:2012-02-19 23:15:26
【问题描述】:

在 WPF 应用程序中,我有一个包含很多字段的窗口。 当用户在填写每个字段后使用 TAB 键时,windows 会理解它会继续下一个。这是众所周知的行为。

现在我想做的是让它模拟 TAB 键,而实际上 RETURN 被击中。 所以在我的 WPF xaml 中,我添加了 imply KeyDown="userPressEnter"

在它背后的代码中:

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

现在,显然这不起作用。但我不知道的是,我该怎么做呢?


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

我发现了一些对我有帮助的东西 =)

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

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

【问题讨论】:

  • 模拟选项卡是指将光标移动到窗口中的下一个字段吗?
  • 是的,将其移至下一个处理程序,以便它可以是字段或按钮。所以模拟与用户点击 TAB 相同的行为。这就是为什么我只是尝试为系统提供 TAB 输入,而实际上按下了 RETURN。
  • 您的解决方案的问题:您需要将其添加到每个控件中,否则 MoveFocus 将无法选择正确的下一个字段。

标签: c# wpf keypress


【解决方案1】:

我认为最好的解决方案是:

var ue = e.OriginalSource as FrameworkElement;

if (e.Key == Key.Return)
{
    e.Handled = true;
    ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}

【讨论】:

    【解决方案2】:

    如何让 SendKeys 类像 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);
                }
            }
        }
    }
    

    【讨论】:

    • 这应该是公认的答案,因为其他答案实际上都不会触发另一个标签按下事件。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      使用 Form 的 SelectNextControl 方法

      【讨论】:

      • 这是一个 WinForms 解决方案
      【解决方案5】:
          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;
          }
      

      【讨论】:

      • 这是一个 winforms 解决方案。 SendKeys 在 System.Windows.Forms dll 中,而不在 System.Windows dll 中
      【解决方案6】:

      您可以在此处查看帖子: http://social.msdn.microsoft.com/Forums/en/wpf/thread/c85892ca-08e3-40ca-ae9f-23396df6f3bd

      这是一个例子:

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

      【讨论】:

      • 呵呵,我也发现了一个 :) 虽然我必须让它有点不同才能让它工作;)
      【解决方案7】:

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

      SendKeys.Send("{TAB}");
      

      代替

      e.Key = Key.Tab
      

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

      【讨论】:

      • 我知道这种方法适用于 winforms 应用程序,但它也适用于 WPF 吗?
      • 你是对的,它没有......我找到了这个,但我确信有更好的解决方案inputsimulator.codeplex.com
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2021-12-26
      • 2018-08-14
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多