gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:56:25 25 4
gpt4 key购买 nike

我正在尝试使用屏幕上的按钮来执行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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com