简体   繁体   English

如何在 Xamarin.Mac 中的应用程序级别捕获按键按下事件

[英]How to capture key down events at application level in Xamarin.Mac

I have a Xamarin.Forms application and successfully capture application level key down events in WPF using the PreviewKeyDown event in the main window.我有一个 Xamarin.Forms 应用程序,并使用主窗口中的 PreviewKeyDown 事件成功捕获了 WPF 中的应用程序级按键按下事件。

How do I do the same thing for Xamarin.Mac in AppDelegate or otherwise?如何在 AppDelegate 或其他方式中为 Xamarin.Mac 做同样的事情?

You can add an NSEvent handler in the AppDelegate's DidFinishLaunching at the local level to monitor any combination of the NSEventMask .您可以在本地级别的 AppDelegate 的DidFinishLaunching中添加NSEvent处理程序,以监视NSEventMask任意组合。

public override void DidFinishLaunching(NSNotification notification)
{
    NSEvent.AddLocalMonitorForEventsMatchingMask(NSEventMask.KeyDown, (NSEvent theEvent) =>
    {
        Console.WriteLine(theEvent);
        return theEvent;
    }); 

    Forms.Init();
    LoadApplication(new App());
    base.DidFinishLaunching(notification);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM