BWhelan
BWhelan

Reputation: 438

How to Inject Keyboard Input of Multiple Keys with Code in UWP

Trying to simulate the pressing of a Tab and Shift+Tab (Reverse Tabbing) keys with functions in the Code Behind so they can be used with Buttons or in this case other keys on the keyboard.

Got the Simulating of Tabbing working, I am just not sure how to simulate the press of 2 keys simultaneously to simulate the Shift+Tab

        private void Tab(string s)
        {
            InputInjector inputInjector = InputInjector.TryCreate();
            var info = new InjectedInputKeyboardInfo();
            if (s == "Right")
            {
                info.VirtualKey = (ushort)(VirtualKey.Tab);
            }
            else if (s == "Left")
            {
                //This doesn't actually exist, but How would I do this?
                info.VirtualKey = (ushort)(VirtualKey.ShiftTab);
            }

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

        private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            switch (e.Key)
            {
                case VirtualKey.Number1:
                    Tab("Left");
                    break;
                case VirtualKey.Q:
                    Tab("Right");
                    break;
            }
        }

So far the Tab works fine when the Q key is pressed, I just need to find out how to do the Shift+Tab for when the Number1 key is pressed. Any help would be greatly appreciated!

Upvotes: 1

Views: 1878

Answers (2)

BWhelan
BWhelan

Reputation: 438

To answer my own question. After some help from another user in a similar question, the easiest way to do simulate a multiple button press is as follows:

            InputInjector inputInjector = InputInjector.TryCreate();
            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 });

In this example I use SHIFT+Tab.

Upvotes: 1

visc
visc

Reputation: 4959

They're called Modifiers

VirtualKey + VirtualKeyModifiers

https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.input.keyboardaccelerator.Modifiers https://learn.microsoft.com/en-us/uwp/api/windows.system.virtualkeymodifiers

If you want to use InputInjector look into ScanCode's as they represent unique identifies for keys on the keyboard- now they might not be the same for all keyboards- but shift key should be the same. I attached a list of common ScanCodes- read the ScanCode to see what value it gives you. InputInjector takes an array so just path both codes you want to inject or find the combined ScanCode

https://learn.microsoft.com/en-us/uwp/api/windows.ui.input.preview.injection.injectedinputkeyboardinfo https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

Try ScanCode for Shift-Tab as 0F

Upvotes: 1

Related Questions