[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [25050] trunk/blender/intern/ghost/intern: Cocoa: proper implementation of the modifiers key wrong value when application becomes active again

Damien Plisson damien.plisson at yahoo.fr
Tue Dec 1 16:46:37 CET 2009


Revision: 25050
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=25050
Author:   damien78
Date:     2009-12-01 16:46:37 +0100 (Tue, 01 Dec 2009)

Log Message:
-----------
Cocoa: proper implementation of the modifiers key wrong value when application becomes active again
Note: this works fine when running under 10.6, even if compiled with an older sdk
Under 10.4/10.5, workaround remains to assume no modifier key is pressed when the user restores the focus to the application

Modified Paths:
--------------
    trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.h
    trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.mm

Modified: trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.h
===================================================================
--- trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.h	2009-12-01 14:48:36 UTC (rev 25049)
+++ trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.h	2009-12-01 15:46:37 UTC (rev 25050)
@@ -200,7 +200,13 @@
      */
     GHOST_TSuccess handleWindowEvent(GHOST_TEventType eventType, GHOST_WindowCocoa* window);
 	
+	/**
+     * Handles the Cocoa event telling the application has become active (again)
+     * @return Indication whether the event was handled. 
+     */
+    GHOST_TSuccess handleApplicationBecomeActiveEvent();
 	
+	
 	/**
      * Handles a drag'n'drop destination event. Called by GHOST_WindowCocoa window subclass
      * @param eventType The type of drag'n'drop event

Modified: trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.mm
===================================================================
--- trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.mm	2009-12-01 14:48:36 UTC (rev 25049)
+++ trunk/blender/intern/ghost/intern/GHOST_SystemCocoa.mm	2009-12-01 15:46:37 UTC (rev 25050)
@@ -392,8 +392,6 @@
 
 #pragma mark Cocoa objects
 
-static bool justGotFocus = false;
-
 /**
  * CocoaAppDelegate
  * ObjC object to capture applicationShouldTerminate, and send quit event
@@ -442,7 +440,7 @@
 
 - (void)applicationWillBecomeActive:(NSNotification *)aNotification
 {
-	justGotFocus = true;
+	systemCocoa->handleApplicationBecomeActiveEvent();
 }
 @end
 
@@ -717,33 +715,11 @@
 
 GHOST_TSuccess GHOST_SystemCocoa::getModifierKeys(GHOST_ModifierKeys& keys) const
 {
-#ifdef MAC_OS_X_VERSION_10_6
-	unsigned int modifiers = [NSEvent modifierFlags];
-
-    keys.set(GHOST_kModifierKeyCommand, (modifiers & NSCommandKeyMask) ? true : false);
-    keys.set(GHOST_kModifierKeyLeftAlt, (modifiers & NSAlternateKeyMask) ? true : false);
-    keys.set(GHOST_kModifierKeyLeftShift, (modifiers & NSShiftKeyMask) ? true : false);
-    keys.set(GHOST_kModifierKeyLeftControl, (modifiers & NSControlKeyMask) ? true : false);
-
-#else
-	if (justGotFocus) {
-			//TODO: need to find a better workaround for the missing cocoa "getModifierFlag" function in 10.4/10.5
-		justGotFocus = false;
-		keys.set(GHOST_kModifierKeyCommand, false);
-		keys.set(GHOST_kModifierKeyLeftAlt, false);
-		keys.set(GHOST_kModifierKeyLeftShift, false);
-		keys.set(GHOST_kModifierKeyLeftControl, false);
-	}
-	else {
-		unsigned int modifiers = [[NSApp currentEvent] modifierFlags];
-		
-		keys.set(GHOST_kModifierKeyCommand, (modifiers & NSCommandKeyMask) ? true : false);
-		keys.set(GHOST_kModifierKeyLeftAlt, (modifiers & NSAlternateKeyMask) ? true : false);
-		keys.set(GHOST_kModifierKeyLeftShift, (modifiers & NSShiftKeyMask) ? true : false);
-		keys.set(GHOST_kModifierKeyLeftControl, (modifiers & NSControlKeyMask) ? true : false);
-	}
-
-#endif
+	keys.set(GHOST_kModifierKeyCommand, (m_modifierMask & NSCommandKeyMask) ? true : false);
+	keys.set(GHOST_kModifierKeyLeftAlt, (m_modifierMask & NSAlternateKeyMask) ? true : false);
+	keys.set(GHOST_kModifierKeyLeftShift, (m_modifierMask & NSShiftKeyMask) ? true : false);
+	keys.set(GHOST_kModifierKeyLeftControl, (m_modifierMask & NSControlKeyMask) ? true : false);
+	
     return GHOST_kSuccess;
 }
 
@@ -873,6 +849,45 @@
     return anyProcessed || m_outsideLoopEventProcessed;
 }
 
+//Note: called from NSApplication delegate
+GHOST_TSuccess GHOST_SystemCocoa::handleApplicationBecomeActiveEvent()
+{
+	//Update the modifiers key mask, as its status may have changed when the application was not active
+	//(that is when update events are sent to another application)
+	unsigned int modifiers;
+	GHOST_IWindow* window = m_windowManager->getActiveWindow();
+
+#ifdef MAC_OS_X_VERSION_10_6
+	modifiers = [NSEvent modifierFlags];
+#else
+	//If build against an older SDK, check if running on 10.6 to use the correct function
+	if ([NSEvent respondsToSelector:@selector(modifierFlags)]) {
+		modifiers = (unsigned int)[NSEvent modifierFlags];
+	}
+	else {
+		//TODO: need to find a better workaround for the missing cocoa "getModifierFlag" function in 10.4/10.5
+		modifiers = 0;
+	}
+#endif
+	
+	if ((modifiers & NSShiftKeyMask) != (m_modifierMask & NSShiftKeyMask)) {
+		pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSShiftKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftShift) );
+	}
+	if ((modifiers & NSControlKeyMask) != (m_modifierMask & NSControlKeyMask)) {
+		pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSControlKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftControl) );
+	}
+	if ((modifiers & NSAlternateKeyMask) != (m_modifierMask & NSAlternateKeyMask)) {
+		pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSAlternateKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyLeftAlt) );
+	}
+	if ((modifiers & NSCommandKeyMask) != (m_modifierMask & NSCommandKeyMask)) {
+		pushEvent( new GHOST_EventKey(getMilliSeconds(), (modifiers & NSCommandKeyMask)?GHOST_kEventKeyDown:GHOST_kEventKeyUp, window, GHOST_kKeyCommand) );
+	}
+	
+	m_modifierMask = modifiers;
+	
+	return GHOST_kSuccess;
+}
+
 //Note: called from NSWindow delegate
 GHOST_TSuccess GHOST_SystemCocoa::handleWindowEvent(GHOST_TEventType eventType, GHOST_WindowCocoa* window)
 {





More information about the Bf-blender-cvs mailing list