英文:
WinUI 3 global key down/up events
问题
在WinUI 3中,您可以通过在页面上处理CoreWindow.KeyDown
事件来处理Escape键按下事件。这需要在页面的构造函数中添加以下代码:
public MainPage()
{
this.InitializeComponent();
CoreWindow.GetForCurrentThread().KeyDown += MainPage_KeyDown;
}
然后,在页面类中添加以下事件处理程序:
private void MainPage_KeyDown(CoreWindow sender, KeyEventArgs args)
{
if (args.VirtualKey == Windows.System.VirtualKey.Escape)
{
// 在这里执行关闭应用的逻辑
}
}
这样,当按下Escape键时,MainPage_KeyDown
方法将被调用,您可以在其中执行关闭应用的逻辑。
英文:
I would like to handle the Escape button key pressed event in a WinUI 3 application, with MVVM Toolkit. I want to use this event to close the application.
I have tried different solutions, but most of them are talking about an UIElement which must have focus.
I have tried the Page KeyDown and KeyUp events, but they only trigger if there is an UIElement in the Page controls, which has focus.
In WPF I could do this by adding a MVVM command with parameter to a KeyBinding in the Windows.InputBindings.
Is there a way to obtain the same functionality in WinUI 3?
答案1
得分: 1
你可以使用KeybordAccelerators
来实现此功能:
<Page.KeyboardAccelerators>
<KeyboardAccelerator
Key="Escape"
Invoked="KeyboardAccelerator_Invoked"
Modifiers="None" />
</Page.KeyboardAccelerators>
private void KeyboardAccelerator_Invoked(
KeyboardAccelerator sender,
KeyboardAcceleratorInvokedEventArgs args)
{
}
更新
如果你需要通过命令来执行操作,你可以使用Microsoft.Xaml.Behaviors.WinUI.Managed NuGet包。
<Page
x:Class="KeyboardAcceleratorsExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:KeyboardAcceleratorsExample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Page.KeyboardAccelerators>
<KeyboardAccelerator
Key="Escape"
Modifiers="None">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Invoked">
<core:InvokeCommandAction Command="{x:Bind ViewModel.DoSomethingCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
</Page.KeyboardAccelerators>
</Page>
英文:
You can use KeybordAccelerators
for this:
<Page.KeyboardAccelerators>
<KeyboardAccelerator
Key="Escape"
Invoked="KeyboardAccelerator_Invoked"
Modifiers="None" />
</Page.KeyboardAccelerators>
private void KeyboardAccelerator_Invoked(
KeyboardAccelerator sender,
KeyboardAcceleratorInvokedEventArgs args)
{
}
UPDATE
If you need to do your process via a command, you can use the Microsoft.Xaml.Behaviors.WinUI.Managed NuGet package.
<Page
x:Class="KeyboardAcceleratorsExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:KeyboardAcceleratorsExample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Page.KeyboardAccelerators>
<KeyboardAccelerator
Key="Escape"
Modifiers="None">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Invoked">
<core:InvokeCommandAction Command="{x:Bind ViewModel.DoSomethingCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
</Page.KeyboardAccelerators>
</Page>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论