c# - 如何使用UWP中的代码模拟Tab键按下

标签 c# xaml mvvm uwp

我正在尝试使用屏幕上的按钮来执行Tab和反向Tab的功能。我希望该应用程序通过“分割”按钮与其他按钮一起切换,然后用户可以使用Enter键选择一个。试图使程序可以通过键盘完全导航。

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

这是我一直在尝试的代码,但这并不是我想要的功能。

        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键放入单击事件或侧面按钮的命令中,而不仅仅是使用方向键。
任何有关此问题的帮助将不胜感激!

最佳答案

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.



键盘输入

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
InputInjector inputInjector = InputInjector.TryCreate();
 var ctrl = new InjectedInputKeyboardInfo();
 ctrl.VirtualKey = (ushort)(VirtualKey.Control);
 ctrl.KeyOptions = InjectedInputKeyOptions.KeyUp;

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

关于c# - 如何使用UWP中的代码模拟Tab键按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56636716/

相关文章:

c# - 'where' 条件无效。实体成员正在调用无效的属性或方法

c# - Windows 8.1 通用应用程序中的 BackgroundWorker 的替代方案是什么?

c# - 如果存在验证错误,也要更新 ViewModel 中的属性

c# - 使用 NPOI 从 Excel 文件中读取图像

C#字典以列表为值,可以直接对列表进行操作吗?

c# - 将对象反序列化为自身

xaml - iOS 上 TabbedPage 中的 Xamarin Forms Secondary ToolbarItem

mvvm - 如何使用 WCF 实现 MVVM?

android - 如何在Android中为MVVM Clean架构创建用例

mvvm - syncfusion 图表不显示数据