问题
I'm trying to build an app that will accept keyboard input from a remote control that emulates a keyboard. I need to capture all keys from the remote, including volume up/down (it emulates a multimedia keyboard, fwiw). I can't figure out how to do that in a UWA. I've tried Windows.UI.Input.KeyboardDeliveryInterceptor and Windows.UI.Core.CoreWindow.GetForCurrentThread().KeyDown, which capture some input, but not all keys (it doesn't capture the special keys).
I don't plan to include this app in the App Store so I can assign any capability that I need, including restricted. I tried to access the HID device directly, but it turns out keyboards are blocked ().
Any ideas?
回答1:
Short answer
The key part is enabling the interceptor in the code behind:
deliveryInterceptor.IsInterceptionEnabledWhenInForeground = true;
and declaring the "inputForegroundObservation" capability in the app manifest:
<rescap:Capability Name="inputForegroundObservation" />
Long answer
Add this to Package.appxmanifest:
Namespace reference to restricted capabilities:
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
Add as child for Capabilities tag:
<rescap:Capability Name="inputForegroundObservation" />
Then add this in your code behind (say MainPage.xaml.cs):
public MainPage()
{
this.InitializeComponent();
var _deliveryInterceptor = KeyboardDeliveryInterceptor.GetForCurrentView();
UpdateTextBox($"Hash interceptor: {_deliveryInterceptor.GetHashCode()} \n");
_deliveryInterceptor.IsInterceptionEnabledWhenInForeground = true;
_deliveryInterceptor.KeyUp += _deliveryInterceptor_KeyEventReceived;
_deliveryInterceptor.KeyDown += _deliveryInterceptor_KeyEventReceived;
}
private void _deliveryInterceptor_KeyEventReceived(KeyboardDeliveryInterceptor sender, Windows.UI.Core.KeyEventArgs
{
//Process KeyUp/KeyDown
}
来源:https://stackoverflow.com/questions/35422422/access-raw-keyboard-input-in-universal-windows-app-iot