【问题标题】:Access raw keyboard input in Universal Windows App (IoT)在通用 Windows 应用 (IoT) 中访问原始键盘输入
【发布时间】:2016-02-16 02:06:58
【问题描述】:
我正在尝试构建一个应用程序,该应用程序将接受来自模拟键盘的遥控器的键盘输入。我需要从遥控器捕获所有键,包括音量增大/减小(它模拟多媒体键盘,fwiw)。我不知道如何在 UWA 中做到这一点。
我试过 Windows.UI.Input.KeyboardDeliveryInterceptor 和 Windows.UI.Core.CoreWindow.GetForCurrentThread().KeyDown,它们捕获一些输入,但不是所有键(它不捕获特殊键)。
我不打算在 App Store 中包含此应用,因此我可以分配我需要的任何功能,包括受限功能。我试图直接访问 HID 设备,但结果是键盘被阻止()。
有什么想法吗?
【问题讨论】:
标签:
win-universal-app
keyboard-events
hid
windows-10-universal
windows-10-iot-core
【解决方案1】:
简答
关键部分是在后面的代码中启用拦截器:
deliveryInterceptor.IsInterceptionEnabledWhenInForeground = true;
并在应用清单中声明“inputForegroundObservation”功能:
<rescap:Capability Name="inputForegroundObservation" />
长答案
将此添加到 Package.appxmanifest:
对受限功能的命名空间引用:
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
添加为 Capabilities 标签的子项:
<rescap:Capability Name="inputForegroundObservation" />
然后在你的代码后面添加这个(比如 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
}