简体   繁体   中英

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.

How do I do the same thing for Xamarin.Mac in AppDelegate or otherwise?

You can add an NSEvent handler in the AppDelegate's DidFinishLaunching at the local level to monitor any combination of the 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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