0

Is there a way to invoke default Windows shortcuts like Win + V programmatically in WinUI UWP?

Some shortcuts, like Win + V, don't have any API calls as far as I know. Hence, I want to invoke them by triggering them programmatically. Is there a solution?

As suggested by @Zer0, I used keyboard input injection. It is working. But, after injection, the physical keyboard starts to malfunction. The keys which I press on the keyboard map to different keys and Windows starts opening random applications. This issue persists for 10 seconds even after closing my application. After 10 seconds, this issue disappears. Why is it behaving so?

By the way, this is my code:

var V = new InjectedInputKeyboardInfo();
V.KeyOptions = InjectedInputKeyOptions.None;
V.VirtualKey = (ushort)VirtualKey.V;
var Win = new InjectedInputKeyboardInfo();
Win.KeyOptions = InjectedInputKeyOptions.None;
Win.VirtualKey = (ushort)VirtualKey.LeftWindows;
injector.InjectKeyboardInput(new[] { Win,V });
4

1 回答 1

1

You might need to release the Windows key after you inject the Win + V successfully.

You could try this:

InputInjector inputInjector = InputInjector.TryCreate();

InjectedInputKeyboardInfo infoV = new InjectedInputKeyboardInfo();
infoV.KeyOptions = InjectedInputKeyOptions.None;
infoV.VirtualKey = (ushort)VirtualKey.V;

// Windows key is an extended key.
InjectedInputKeyboardInfo infoW = new InjectedInputKeyboardInfo();
infoW.KeyOptions = InjectedInputKeyOptions.ExtendedKey;
infoW.VirtualKey = (ushort)VirtualKey.LeftWindows;

inputInjector.InjectKeyboardInput(new[] { infoW, infoV });

// Release the key
infoW.KeyOptions = InjectedInputKeyOptions.KeyUp;
infoW.VirtualKey = (ushort)VirtualKey.LeftWindows;

inputInjector.InjectKeyboardInput(new[] { infoW });
于 2021-08-20T06:17:02.033 回答