如何在 UWP 中使用代码模拟 Tab 键按下

如何在 UWP 中使用代码模拟 Tab 键按下

问题描述:

我正在尝试使用屏幕上的按钮来执行 Tab 和反向 Tab 的功能.我希望应用程序使用这些其他按钮在 Segment 按钮之间切换,然后用户可以使用 Enter 键选择一个.试图使程序完全可以通过键盘导航.

I am trying to use buttons on the screen to perform the function of Tab and reverse Tab. I want the app to tab through the Segment buttons with these other buttons and then the user be able to select one with the Enter key. Trying to make the program fully navigable by keyboard.

我尝试使用 KeyDown 事件,然后使用定向焦点导航,但我更希望系统模拟 Tab 键按下以跟随 Tab 路径.

I have tried using a KeyDown event and then directional focus navigation, but I am more looking to have the system simulate a tab key press to follow the tab path.

这是我一直在尝试解决的代码,但它并不是我想要的功能.

This is the code I have been trying to far as a work around but it isn't exactly the functionality I want.

        private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            DependencyObject candidate = null;

            var options = new FindNextElementOptions()
            {
                SearchRoot = WeldPGrid,
                XYFocusNavigationStrategyOverride = XYFocusNavigationStrategyOverride.Projection
            };

            switch (e.Key)
            {
                case Windows.System.VirtualKey.Up:
                    candidate =
                        FocusManager.FindNextElement(
                            FocusNavigationDirection.Up, options);
                    break;
                case Windows.System.VirtualKey.Down:
                    candidate =
                        FocusManager.FindNextElement(
                            FocusNavigationDirection.Down, options);
                    break;
                case Windows.System.VirtualKey.Left:
                    candidate = FocusManager.FindNextElement(
                        FocusNavigationDirection.Left, options);
                    break;
                case Windows.System.VirtualKey.Right:
                    candidate =
                        FocusManager.FindNextElement(
                            FocusNavigationDirection.Right, options);
                    break;
            }
            // Also consider whether candidate is a Hyperlink, WebView, or TextBlock.
            if (candidate != null && candidate is Control)
            {
                (candidate as Control).Focus(FocusState.Keyboard);
            }
        }

作为最终结果,我希望将模拟的 Tab 按下放在侧面按钮的单击事件或命令中,而不仅仅是使用方向键.对此问题的任何帮助将不胜感激!

As a final result I am looking to put the simulated tab press in a click event or command for the buttons on the side instead of just using the directional keys. Any help with this issue would be much appreciated!

但看起来这个库可能有一种新的方法,我只需要弄清楚如何使用它

But it looks like there might be a new way available with this library I just gotta figure out how to use it

在使用输入注入之前,我们必须在应用程序清单中声明此功能,因为它是非标准功能.这是一项受限功能,这意味着您可以使用它安全地将您的应用发布到应用商店,但需要获得商店提交的批准.

Before we utilize input injection, we have to declare this capability in the application manifest, as it is a non-standard functionality. It is a restricted capability, which means you can safely publish your app into the app store with it, but require approval for store submission.

键盘输入

InjectedInputKeyboardInfo 类将作为键盘输入注入的基础.最重要的属性是 VirtualKey,它指定与输入相关的键.使用 KeyOptions,我们可以指定更多选项,例如模拟按键事件.

The InjectedInputKeyboardInfo class will the the base for keyboard input injection. The most important property is the VirtualKey, which specifies which key is the input related to. Using KeyOptions we can specify further options like simulating key up event.

private async void Button_Click(object sender, RoutedEventArgs e)
{
    InputInjector inputInjector = InputInjector.TryCreate();
    for (int i = 0; i < 10; i++)
    {
        var info = new InjectedInputKeyboardInfo();
        info.VirtualKey = (ushort)(VirtualKey.Tab);
        inputInjector.InjectKeyboardInput(new[] { info });
        await Task.Delay(1000);
    }        
}

更新

Shift+Tab

InputInjector inputInjector = InputInjector.TryCreate();
 for (int i = 0; i < 10; i++)
 {
     var shift = new InjectedInputKeyboardInfo();
     shift.VirtualKey = (ushort)(VirtualKey.Shift);
     shift.KeyOptions = InjectedInputKeyOptions.None;


     var tab = new InjectedInputKeyboardInfo();
     tab.VirtualKey = (ushort)(VirtualKey.Tab);
     tab.KeyOptions = InjectedInputKeyOptions.None;


     inputInjector.InjectKeyboardInput(new[] { shift,tab});

     await Task.Delay(1000);
 }

更新 1

为了释放key,我们需要将key选项设置为KeyUp并再次调用InjectKeyboardInput.

For releasing key, we need set the key option as KeyUp and invoke InjectKeyboardInput again.

InputInjector inputInjector = InputInjector.TryCreate();
 var ctrl = new InjectedInputKeyboardInfo();
 ctrl.VirtualKey = (ushort)(VirtualKey.Control);
 ctrl.KeyOptions = InjectedInputKeyOptions.KeyUp;

 inputInjector.InjectKeyboardInput(new[] { ctrl });