为什么以编程方式使用键盘时会出现未经授权的访问?

Why do I get unauthorized access while using keyboard programmatically?

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 });
shift.KeyOptions = InjectedInputKeyOptions.KeyUp;
tab.KeyOptions = InjectedInputKeyOptions.KeyUp;

请查看这张图片并告诉我应该如何解决这个问题

如果您查看 InputInjector.TryCreate() 方法的 docs,它会显示:

Note: The APIs in this namespace require the inputInjectionBrokered restricted capability.

您需要更新 Package.appxmanifest 才能添加该功能,例如:

<?xml version="1.0" encoding="utf-8"?>
<Package
    ...
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="... rescap">
...
<Capabilities>
    <rescap:Capability Name="inputInjectionBrokered"/>
</Capabilities>
</Package>