2

I need to subclass NSApplication in MonoMac to override the sendEvent (SendEvent in C# terms) method of NSApplication in order to receive media key events (Play, Pause, Next, Prev on Macbooks) as described here: Listening to mac keyboard play/pause events and here: https://bitbucket.org/nkreeger/whitedragon/src/6f530c8a34a7/component/sbAppleMediaKeyController.mm

So far I came up with this code:

class MainClass
{
    static void Main (string[] args)
    {
        MyApp.Init ();
        MyApp.Main (args);
    }
}
class MyApp : NSApplication
{
    public override void SendEvent (NSEvent theEvent)
    {
        Console.WriteLine ("event received!");
        base.SendEvent (theEvent);
    }
    public override void SetMainMenu (NSMenu aMenu)
    {
        Console.WriteLine ("main menu set!");
        base.SetMainMenu (aMenu);
    }
}

But neither SendEvent or SetMainMenu (which I added for testing purposes as SetMainMenu should be called once on startup) are called, as I don't get any console output.

What is the correct way of doing this?

Community
  • 1
  • 1
Dynalon
  • 6,577
  • 10
  • 54
  • 84
  • I would try a different debugging output method other than Console.WriteLine(). Write to a file, popup a dialog, or use System.Diagnostics.Trace methods (you can install your own implementation of the trace target). – Les Aug 11 '12 at 14:48
  • Hi Les, did you ever figure this out? – ClaraU Jan 05 '15 at 10:41

1 Answers1

6

Just subclassing NSApplication is not sufficient for your subclass to replace the normal one. You have to setup your NSPrincipalClass in your application plist as well.

See:

https://developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

Julien
  • 3,427
  • 20
  • 22