Index: /trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk	(revision 16609)
+++ /trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk	(revision 16610)
@@ -206,6 +206,7 @@
 # r=bird: what is -lz doing here? it belongs in LIBS.
 VirtualBox_LDFLAGS.darwin = -lz \
-	-framework IOKit -framework AppKit -framework ApplicationServices -framework Foundation -framework Carbon -framework QuickTime \
+	-framework IOKit -framework AppKit -framework ApplicationServices -framework Foundation -framework Carbon \
 	$(if $(VBOX_WITH_HARDENING),-install_name $(VBOX_DYLD_EXECUTABLE_PATH)/VirtualBox.dylib)
+VirtualBox_LDFLAGS.darwin.x86 = -framework QuickTime
 ifdef VBOX_WITH_ICHAT_THEATER
  VirtualBox_LDFLAGS.darwin += -framework InstantMessage -framework QuartzCore
@@ -391,6 +392,9 @@
 ifdef VBOX_WITH_ICHAT_THEATER
  VirtualBox_SOURCES.darwin += \
-	 src/darwin/VBoxIChatTheaterWrapper.m
-endif
+ 	src/darwin/VBoxIChatTheaterWrapper.m
+endif
+
+VirtualBox_SOURCES.darwin.amd64 += \
+	src/darwin/VBoxCocoaApplication.m
 
 # The Qt modules we're using.
Index: /trunk/src/VBox/Frontends/VirtualBox/include/QIHotKeyEdit.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/include/QIHotKeyEdit.h	(revision 16609)
+++ /trunk/src/VBox/Frontends/VirtualBox/include/QIHotKeyEdit.h	(revision 16610)
@@ -28,5 +28,5 @@
 #include <QMap>
 #endif
-#if defined (Q_WS_MAC) && !defined (QT_MAC_USE_COCOA)
+#if defined (Q_WS_MAC)
 # include <Carbon/Carbon.h>
 #endif
@@ -82,7 +82,10 @@
 #elif defined (Q_WS_X11)
     bool x11Event (XEvent *event);
-#elif defined (Q_WS_MAC) && !defined (QT_MAC_USE_COCOA)
-    static pascal OSStatus darwinEventHandlerProc (EventHandlerCallRef inHandlerCallRef,
-                                                   EventRef inEvent, void *inUserData);
+#elif defined (Q_WS_MAC)
+# ifdef QT_MAC_USE_COCOA
+    static bool darwinEventHandlerProc (const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
+# else
+    static pascal OSStatus darwinEventHandlerProc (EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData);
+# endif
     bool darwinKeyboardEvent (EventRef inEvent);
 #endif
@@ -106,7 +109,9 @@
 #endif
 
-#if defined (Q_WS_MAC) && !defined (QT_MAC_USE_COCOA)
+#if defined (Q_WS_MAC)
+# ifndef QT_MAC_USE_COCOA
     /** Event handler reference. NULL if the handler isn't installed. */
     EventHandlerRef mDarwinEventHandlerRef;
+# endif
     /** The current modifier key mask. Used to figure out which modifier
      *  key was pressed when we get a kEventRawKeyModifiersChanged event. */
Index: /trunk/src/VBox/Frontends/VirtualBox/src/QIHotKeyEdit.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/QIHotKeyEdit.cpp	(revision 16609)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/QIHotKeyEdit.cpp	(revision 16610)
@@ -71,4 +71,7 @@
 # include "DarwinKeyboard.h"
 # include <Carbon/Carbon.h>
+# ifdef QT_MAC_USE_COCOA
+#  include "darwin/VBoxCocoaApplication.h"
+# endif
 #endif
 
@@ -133,9 +136,8 @@
 
 #ifdef Q_WS_MAC
+    mDarwinKeyModifiers = GetCurrentEventKeyModifiers();
 # ifdef QT_MAC_USE_COCOA
-/** @todo Carbon -> Cocoa */
+    ::VBoxCocoaApplication_setCallback(UINT32_MAX, QIHotKeyEdit::darwinEventHandlerProc, this);
 # else  /* !QT_MAC_USE_COCOA */
-    mDarwinKeyModifiers = GetCurrentEventKeyModifiers();
-
     EventTypeSpec eventTypes [4];
     eventTypes [0].eventClass = kEventClassKeyboard;
@@ -164,5 +166,5 @@
     ::DarwinReleaseKeyboard();
 # ifdef QT_MAC_USE_COCOA
-    /** @todo Carbon -> Cocoa */
+    ::VBoxCocoaApplication_unsetCallback(UINT32_MAX, QIHotKeyEdit::darwinEventHandlerProc, this);
 # else
     ::RemoveEventHandler (mDarwinEventHandlerRef);
@@ -667,7 +669,16 @@
 #elif defined (Q_WS_MAC)
 # ifdef QT_MAC_USE_COCOA
-/** @todo Carbon -> Cocoa */
-# else /* !QT_MAC_USE_COCOA */
-
+/* static */
+bool QIHotKeyEdit::darwinEventHandlerProc (const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser)
+{
+    QIHotKeyEdit *edit = (QIHotKeyEdit *) pvUser;
+    EventRef inEvent = (EventRef)pvCarbonEvent;
+    UInt32 EventClass = ::GetEventClass (inEvent);
+    if (EventClass == kEventClassKeyboard)
+        return edit->darwinKeyboardEvent (inEvent);
+    return false;
+}
+
+# else  /* !QT_MAC_USE_COCOA */
 /* static */
 pascal OSStatus QIHotKeyEdit::darwinEventHandlerProc (EventHandlerCallRef inHandlerCallRef,
@@ -683,4 +694,5 @@
     return CallNextEventHandler (inHandlerCallRef, inEvent);
 }
+# endif /* !QT_MAC_USE_COCOA */
 
 bool QIHotKeyEdit::darwinKeyboardEvent (EventRef inEvent)
@@ -725,5 +737,4 @@
     return false;
 }
-# endif /* !QT_MAC_USE_COCOA */
 
 #else
Index: /trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxCocoaApplication.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxCocoaApplication.h	(revision 16610)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxCocoaApplication.h	(revision 16610)
@@ -0,0 +1,92 @@
+/* $Id$ */
+/** @file
+ * VBoxCocoaApplication - NSApplication subclass for handling -sendEvent.
+ */
+
+/*
+ * Copyright (C) 2009 Sun Microsystems, Inc.
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
+ * Clara, CA 95054 USA or visit http://www.sun.com if you need
+ * additional information or have any questions.
+ *
+ */
+
+#ifndef ___darwin_VBoxCocoaApplication_h
+#define ___darwin_VBoxCocoaApplication_h
+
+#include <iprt/cdefs.h>
+#ifdef __OBJC__
+# import <AppKit/NSApplication.h>
+#endif
+#include <Carbon/Carbon.h>
+
+__BEGIN_DECLS
+
+/** Event handler callback.
+ * @returns true if handled, false if not.
+ * @param   pvCocoaEvent    The Cocoa event.
+ * @param   pvCarbonEvent   The Carbon event.
+ * @param   pvUser          The user argument.
+ */
+typedef bool (*PFNVBOXCACALLBACK)(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
+
+
+#ifdef __OBJC__
+
+/** Structure for tracking a callback. */
+typedef struct VBOXCAENTRY
+{
+    /** Mask of events to send to this callback. */
+    uint32_t            fMask;
+    /** The callback. */
+    PFNVBOXCACALLBACK   pfnCallback;
+    /** The user argument. */
+    void               *pvUser;
+} VBOXCAENTRY;
+typedef VBOXCAENTRY *PVBOXCAENTRY;
+typedef VBOXCAENTRY const *PCVBOXCAENTRY;
+
+
+/**
+ * Subclass for intercepting sendEvent messages.
+ */
+@interface VBoxCocoaApplication : NSApplication
+{
+    /** The event mask for which there currently are callbacks. */
+    uint32_t    m_fMask;
+    /** The number of current callbacks. */
+    uint32_t    m_cCallbacks;
+    /** Array of callbacks. */
+    VBOXCAENTRY m_aCallbacks[4];
+}
+- (void)sendEvent:(NSEvent *)theEvent;
+- (void)setCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
+- (void)unsetCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
+
+@end /* @interface VBoxCocoaApplication */
+
+extern VBoxCocoaApplication *g_pVBoxCocoaApp;
+
+#endif /* __OBJC__ */
+
+/** @name The C/C++ interface.
+ * @{
+ */
+void VBoxCocoaApplication_sharedApplication(void);
+void VBoxCocoaApplication_setCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
+void VBoxCocoaApplication_unsetCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
+/** @} */
+
+__END_DECLS
+
+#endif
+
Index: /trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxCocoaApplication.m
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxCocoaApplication.m	(revision 16610)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxCocoaApplication.m	(revision 16610)
@@ -0,0 +1,198 @@
+/* $Id$ */
+/** @file
+ * VBoxCocoaApplication - NSApplication subclass for handling -sendEvent.
+ */
+
+/*
+ * Copyright (C) 2009 Sun Microsystems, Inc.
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
+ * Clara, CA 95054 USA or visit http://www.sun.com if you need
+ * additional information or have any questions.
+ *
+ */
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include "VBoxCocoaApplication.h"
+#include <iprt/assert.h>
+#import <AppKit/NSEvent.h>
+
+#include <stdio.h>
+
+
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+/** Pointer to the VBoxCocoaApplication instance.
+ * This is also available thru NSApp, but this way there is no need to cast any thing.
+ */
+VBoxCocoaApplication *g_pVBoxCocoaApp = NULL;
+
+@implementation VBoxCocoaApplication
+
+
+-(id) init;
+{
+    self = [super init];
+    self->m_cCallbacks = 0;
+    return self;
+}
+
+
+-(void) sendEvent:(NSEvent *)pEvent;
+{
+    /*
+     * Check if the type matches any of the registered callbacks.
+     */
+    uint32_t const  fMask = self->m_fMask;
+    if (fMask != 0)
+    {
+        NSEventType EvtType = [pEvent type];
+        uint32_t    fEvtMask = RT_LIKELY(EvtType < 32) ? RT_BIT_32(EvtType) : 0;
+        if (fMask & fEvtMask)
+        {
+            /*
+             * Do the callouts in LIFO order.
+             */
+            int i = self->m_cCallbacks;
+            PCVBOXCAENTRY pCur = &self->m_aCallbacks[i];
+            while (i-- > 0)
+            {
+                pCur--;
+                if (pCur->fMask & fEvtMask)
+                {
+                    if (pCur->pfnCallback(pEvent, [pEvent eventRef], pCur->pvUser))
+                        return;
+                }
+            }
+        }
+    }
+
+printf("EvtType=%d modifierFlags=%#x\n", (int)[pEvent type], (unsigned)[pEvent modifierFlags]);
+//    NSKeyDown                   = 10,
+//    NSKeyUp                     = 11,
+//    NSFlagsChanged              = 12,
+
+    /*
+     * Get on with it.
+     */
+    [super sendEvent:pEvent];
+}
+
+
+/**
+ * Register an event callback.
+ *
+ * @param   fMask           The event mask for which the callback is to be invoked.
+ * @param   pfnCallback     The callback function.
+ * @param   pvUser          The user argument.
+ */
+-(void) setCallback: (uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser
+{
+    unsigned i = self->m_cCallbacks;
+    AssertReleaseReturnVoid(i < RT_ELEMENTS(self->m_aCallbacks));
+
+    self->m_aCallbacks[i].pfnCallback = pfnCallback;
+    self->m_aCallbacks[i].pvUser      = pvUser;
+    self->m_aCallbacks[i].fMask       = fMask;
+    self->m_cCallbacks++;
+    self->m_fMask |= fMask;
+}
+
+
+/**
+ * Deregister an event callback.
+ *
+ * @param   fMask           Same as setCallback.
+ * @param   pfnCallback     Same as setCallback.
+ * @param   pvUser          Same as setCallback.
+ */
+-(void) unsetCallback: (uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser
+{
+    /*
+     * Loop the event array LIFO fashion searching for a matching callback.
+     */
+    int i = self->m_cCallbacks;
+    while (i-- > 0)
+    {
+        if (    self->m_aCallbacks[i].pfnCallback == pfnCallback
+            &&  self->m_aCallbacks[i].pvUser      == pvUser
+            &&  self->m_aCallbacks[i].fMask       == fMask)
+        {
+            uint32_t fNewMask;
+
+            if (i + 1 != self->m_cCallbacks)
+                self->m_aCallbacks[i] = self->m_aCallbacks[self->m_cCallbacks - 1];
+            self->m_cCallbacks--;
+
+            /*
+             * Recalculate the event type mask.
+             */
+            fNewMask = 0;
+            i = self->m_cCallbacks;
+            while (i-- > 0)
+                fNewMask |= self->m_aCallbacks[i].fMask;
+            self->m_fMask = fNewMask;
+            return;
+        }
+    }
+    AssertFailed();
+}
+
+
+@end /* @implementation VBoxCocoaApplication */
+
+
+/**
+ * C/C++ interface for calling VBoxCocoaApplication::sharedApplication.
+ *
+ */
+void VBoxCocoaApplication_sharedApplication(void)
+{
+    if (!g_pVBoxCocoaApp)
+    {
+        /*
+         * It is essential that we use the inherited sharedApplication class
+         * method, otherwise we'll be screwed later by static variables in it.
+         */
+        NSApplication *pApp = [VBoxCocoaApplication sharedApplication];
+        g_pVBoxCocoaApp = (VBoxCocoaApplication *)pApp;
+    }
+}
+
+
+/**
+ * Register an event callback.
+ *
+ * @param   fMask           The event mask for which the callback is to be invoked.
+ * @param   pfnCallback     The callback function.
+ * @param   pvUser          The user argument.
+ */
+void VBoxCocoaApplication_setCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser)
+{
+    [g_pVBoxCocoaApp setCallback:fMask :pfnCallback :pvUser];
+}
+
+
+/**
+ * Deregister an event callback.
+ *
+ * @param   fMask           Same as setCallback.
+ * @param   pfnCallback     Same as setCallback.
+ * @param   pvUser          Same as setCallback.
+ */
+void VBoxCocoaApplication_unsetCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser)
+{
+    [g_pVBoxCocoaApp unsetCallback:fMask :pfnCallback :pvUser];
+}
+
Index: /trunk/src/VBox/Frontends/VirtualBox/src/main.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/main.cpp	(revision 16609)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/main.cpp	(revision 16610)
@@ -25,9 +25,12 @@
 #include "VBoxSelectorWnd.h"
 #include "VBoxConsoleWnd.h"
-#ifdef Q_WS_MAC
+#include "VBoxUtils.h"
+#if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
 # include "QIApplication.h"
-# include "VBoxUtils.h"
 #else
 # define QIApplication QApplication
+#endif
+#ifdef QT_MAC_USE_COCOA
+# include "darwin/VBoxCocoaApplication.h"
 #endif
 
@@ -234,4 +237,10 @@
     sigaction (SIGBUS, &sa, NULL);
     sigaction (SIGUSR1, &sa, NULL);
+#endif
+
+#ifdef QT_MAC_USE_COCOA
+    /* Instantiate our NSApplication derivative before QApplication
+     * forces NSApplication to be instantiated. */
+    VBoxCocoaApplication_sharedApplication();
 #endif
 
