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

MAUI中如何在Windows平台捕获KeyUp事件?

在MAUI Windows平台捕获KeyUp事件的实现方法

在MAUI的Windows平台(基于WinUI 3)中,你可以通过以下方式捕获全局KeyUp事件:

  • 打开Windows项目下的Platforms/Windows/App.xaml.cs文件,找到OnLaunched方法,在窗口初始化完成后获取当前窗口实例并订阅KeyUp事件:
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    base.OnLaunched(args);

    var currentWindow = Microsoft.UI.Xaml.Window.Current;
    if (currentWindow != null)
    {
        currentWindow.CoreWindow.KeyUp += CoreWindow_KeyUp;
    }
}

private void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    // 在这里处理按键抬起逻辑
    // 示例:判断按下的是ESC键
    if (args.VirtualKey == Windows.System.VirtualKey.Escape)
    {
        // 执行对应操作
    }
}
  • 注意:MAUI Windows的窗口对象是Microsoft.UI.Xaml.Window,而非UWP的Windows.UI.Xaml.Window,但通过CoreWindow属性仍能访问到底层的窗口输入事件。如果需要在共享代码层处理按键逻辑,可以结合依赖服务封装平台特定代码,但直接在Windows项目中实现是最直接的方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:14:52