简体   繁体   English

禁用与 UWP 应用程序的键盘交互

[英]Disable keyboard interaction with UWP app

I am building a UWP application and I wish to disable the keyboard interaction with my application.我正在构建一个 UWP 应用程序,我希望禁用与我的应用程序的键盘交互。 This means my app should not in any way respond when any key on the keyboard is pressed.这意味着当按下键盘上的任何键时,我的应用程序不应以任何方式响应。

Is this achievable?这是可以实现的吗? Can I selectively disable interaction with some keys like the Tab key etc.我可以有选择地禁用与某些键(如 Tab 键等)的交互吗?

Yes, you can use the KeyboardDeliveryInterceptor class for this.是的,您可以为此使用KeyboardDeliveryInterceptor类。 Couple of things to note with this:与此有关的几点注意事项:

  1. You will need to declare restricted capability 'inputForegroundObservation' in your appxmanifest file:您需要在 appxmanifest 文件中声明受限功能“inputForegroundObservation”:
<Capabilities>
  <Capability Name="internetClient" />
  <rescap:Capability xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" Name="inputForegroundObservation"/>
</Capabilities>
  1. You cannot intercept keys selectively, but you can respond to particular intercepted keys in your code and respond with the desired action (eg move focus when the tab key is pressed):您不能有选择地截取键,但您可以响应代码中特定截取的键并响应所需的操作(例如,按下 Tab 键时移动焦点):
KeyboardDeliveryInterceptor interceptor = KeyboardDeliveryInterceptor.GetForCurrentView();
interceptor.IsInterceptionEnabledWhenInForeground = true;
interceptor.KeyUp += delegate(KeyboardDeliveryInterceptor sender, KeyEventArgs args)
{
    if (args.VirtualKey == Windows.System.VirtualKey.Tab)
    {
        // perform desired tab key action
    }
};
<Capabilities>
     <Capability Name="internetClient" />
     <rescap:Capability xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" Name="inputForegroundObservation"/>
</Capabilities>

Add permission as Stefan said above.添加权限,如 Stefan 上面所说。 After you add permission add below code.添加权限后添加以下代码。

Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;

private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
    {
        if(args.key == Windows.System.VirtualKey.Tab)
        {
           args.Handled = true;
        }
    }

The above code prevents the Tab button to perform any action.上面的代码阻止 Tab 按钮执行任何操作。 Hence, When the user presses Tab key no action will be performed.因此,当用户按下 Tab 键时,不会执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM