Access raw keyboard input in Universal Windows App (IoT)

99封情书 提交于 2019-12-10 11:51:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!