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

时间:2019-05-03 12:16:22

标签: macos xamarin xamarin.forms xamarin.mac

我有一个Xamarin.Forms应用程序,并使用主窗口中的PreviewKeyDown事件成功捕获了WPF中的应用程序级按键按下事件。

如何在AppDelegate中对Xamarin.Mac进行同样的处理?

1 个答案:

答案 0 :(得分:1)

您可以在本地级别的AppDelegate的NSEvent中添加DidFinishLaunching处理程序,以监视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);
}