WinUI 3 全局按键按下/松开事件

huangapple go评论49阅读模式
英文:

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>

huangapple
  • 本文由 发表于 2023年5月13日 15:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定