禁用与 UWP 应用的键盘交互
Disable keyboard interaction with UWP app
我正在构建一个 UWP 应用程序,我希望禁用与我的应用程序的键盘交互。这意味着当按下键盘上的任何键时,我的应用程序不应以任何方式响应。
这可以实现吗?我可以有选择地禁用与某些键(如 Tab 键等)的交互吗?
是的,您可以为此使用 KeyboardDeliveryInterceptor class。有几点需要注意:
- 您需要在您的 appxmanifest 文件中声明受限功能 'inputForegroundObservation':
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" Name="inputForegroundObservation"/>
</Capabilities>
- 您不能有选择地拦截键,但您可以响应代码中特定的拦截键并响应所需的操作(例如,按下 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>
按照 Stefan 上面所说的添加权限。添加权限后添加以下代码。
Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
if(args.key == Windows.System.VirtualKey.Tab)
{
args.Handled = true;
}
}
以上代码阻止 Tab 按钮执行任何操作。因此,当用户按下 Tab 键时,不会执行任何操作。
我正在构建一个 UWP 应用程序,我希望禁用与我的应用程序的键盘交互。这意味着当按下键盘上的任何键时,我的应用程序不应以任何方式响应。
这可以实现吗?我可以有选择地禁用与某些键(如 Tab 键等)的交互吗?
是的,您可以为此使用 KeyboardDeliveryInterceptor class。有几点需要注意:
- 您需要在您的 appxmanifest 文件中声明受限功能 'inputForegroundObservation':
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" Name="inputForegroundObservation"/>
</Capabilities>
- 您不能有选择地拦截键,但您可以响应代码中特定的拦截键并响应所需的操作(例如,按下 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>
按照 Stefan 上面所说的添加权限。添加权限后添加以下代码。
Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
if(args.key == Windows.System.VirtualKey.Tab)
{
args.Handled = true;
}
}
以上代码阻止 Tab 按钮执行任何操作。因此,当用户按下 Tab 键时,不会执行任何操作。