You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Maui(Windows)如何注册KeyDown/KeyUp键盘事件?

.NET Maui Windows平台全局键盘事件捕获方案

针对Windows平台,你可以直接通过WinUI的窗口实例来注册KeyDown和KeyUp事件,实现类似WinForms的全局键盘监听效果,以下是两种常用实现方式:

1. 应用级全局监听(推荐)

在Windows平台专属代码文件Platforms/Windows/App.xaml.cs中,重写OnLaunched方法获取主窗口并绑定事件:

using Microsoft.UI.Xaml;
using Windows.System;

namespace YourAppName.Windows;

public partial class App : MauiWinUIApplication
{
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        base.OnLaunched(args);

        var winuiMainWindow = Window.Current;
        if (winuiMainWindow != null)
        {
            winuiMainWindow.KeyDown += WinuiMainWindow_KeyDown;
            winuiMainWindow.KeyUp += WinuiMainWindow_KeyUp;
        }
    }

    private void WinuiMainWindow_KeyUp(Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        // 处理键盘松开事件,示例:监听Esc键
        if (e.Key == VirtualKey.Escape)
        {
            // 执行你的业务逻辑
        }
    }

    private void WinuiMainWindow_KeyDown(Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        // 处理键盘按下事件,示例:监听Ctrl+C组合键
        if (e.Key == VirtualKey.C && e.KeyModifiers == VirtualKeyModifiers.Control)
        {
            // 执行你的业务逻辑
        }
    }

    // 其他默认代码...
}

2. 页面级局部监听

如果只需要在特定页面捕获键盘事件,可在页面后台代码中通过HandlerChanged事件获取WinUI窗口实例,并在页面销毁时取消订阅避免内存泄漏:

using Microsoft.UI.Xaml;
using Windows.System;

namespace YourAppName.Views;

public partial class YourPage : ContentPage
{
    public YourPage()
    {
        InitializeComponent();
    }

    protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();

#if WINDOWS
        if (Handler?.PlatformView is FrameworkElement platformElement)
        {
            var window = platformElement.XamlRoot?.Content as Window;
            if (window != null)
            {
                window.KeyDown += PageWindow_KeyDown;
                window.KeyUp += PageWindow_KeyUp;
            }
        }
#endif
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

#if WINDOWS
        if (Handler?.PlatformView is FrameworkElement platformElement)
        {
            var window = platformElement.XamlRoot?.Content as Window;
            if (window != null)
            {
                window.KeyDown -= PageWindow_KeyDown;
                window.KeyUp -= PageWindow_KeyUp;
            }
        }
#endif
    }

#if WINDOWS
    private void PageWindow_KeyUp(Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        // 页面级键盘松开逻辑
    }

    private void PageWindow_KeyDown(Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        // 页面级键盘按下逻辑
    }
#endif
}

注意事项

  • 以上代码仅针对Windows平台,通过#if WINDOWS条件编译隔离,不影响其他平台编译
  • 事件参数KeyRoutedEventArgs可直接获取按键信息,包括组合键(通过KeyModifiers属性)
  • 页面级监听务必在OnDisappearing中取消事件订阅,防止内存泄漏

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 09:55:13