简体   繁体   中英

How to Simulate a Tab Key Press with Code in UWP

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.

演示版面

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

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.

Keyboard input

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

Update

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

Update 1

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

You can use the WIN32 API, get the handle of your application and use SendKeys.SendWait("{Tab}");

EX:

IntPtr handle = FindWindow(null, "YourApplicationName");
SetForegroundWindow(handle);
SendKeys.SendWait("{Tab}");
SendKeys.Flush();

If your tab order is set up correctly it will tab through your controls how many times you specify. Although this simulates actual input so it may not place nice if you expect users to provide input on top of the system tabbing.

here is a helpful link if you want to learn more about the WIN32 API and the simulation of mouse and keyboard input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM