Correct event behaviour on certain control keys
authoroliver <oliver@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Nov 2007 23:30:25 +0000 (23:30 +0000)
committeroliver <oliver@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Nov 2007 23:30:25 +0000 (23:30 +0000)
Reviewed by Geoff

Make sure we send the correct keyDown and keyUp events for the
control keys CapsLock, Shift, Ctrl, Alt, and Meta/Command, and
uses Windows key codes for the event keyCode.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@27372 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/WebCore.base.exp
WebCore/page/EventHandler.cpp
WebCore/platform/PlatformKeyboardEvent.h
WebCore/platform/gtk/KeyEventGtk.cpp
WebCore/platform/mac/KeyEventMac.mm
WebCore/platform/win/KeyEventWin.cpp
WebCore/platform/wx/KeyEventWin.cpp
WebKit/WebView/WebHTMLView.mm

index beb1c63bbbb55d80727d61d4022a5ba56057c458..83c4b377ae231812ce8a6f07766ef3424f40c9b1 100644 (file)
@@ -1,3 +1,31 @@
+2007-11-01  Oliver Hunt  <oliver@apple.com>
+
+        Reviewed by Geoff.
+
+        Correct event behaviour on certain control keys
+
+        Make sure we send the correct keyDown and keyUp events for the
+        control keys CapsLock, Shift, Ctrl, Alt, and Meta/Command, and
+        uses Windows key codes for the event keyCode.
+
+        * WebCore.base.exp:
+        * page/EventHandler.cpp:
+        (WebCore::EventHandler::keyEvent):
+        * platform/PlatformKeyboardEvent.h:
+        * platform/gtk/KeyEventGtk.cpp:
+        (WebCore::PlatformKeyboardEvent::PlatformKeyboardEvent):
+        * platform/mac/KeyEventMac.mm:
+        (WebCore::keyIdentifierForKeyEvent):
+        (WebCore::WindowsKeyCodeForKeyEvent):
+        (WebCore::isKeyUpEvent):
+        (WebCore::textFromEvent):
+        (WebCore::unmodifiedTextFromEvent):
+        (WebCore::PlatformKeyboardEvent::PlatformKeyboardEvent):
+        * platform/win/KeyEventWin.cpp:
+        (WebCore::PlatformKeyboardEvent::PlatformKeyboardEvent):
+        * platform/wx/KeyEventWin.cpp:
+        (WebCore::PlatformKeyboardEvent::PlatformKeyboardEvent):
+
 2007-11-01  Timothy Hatcher  <timothy@apple.com>
 
         Reviewed by Sam.
index 6b93262aaad52b0c0388a18a8676b2b33ddd75b4..e9fa03eda1874aaa6fd762c2a5223f9ccf9a7429 100644 (file)
@@ -208,6 +208,7 @@ __ZN7WebCore12EventHandler20hitTestResultAtPointERKNS_8IntPointEb
 __ZN7WebCore12EventHandler20sendContextMenuEventERKNS_18PlatformMouseEventE
 __ZN7WebCore12EventHandler27capsLockStateMayHaveChangedEv
 __ZN7WebCore12EventHandler7mouseUpEP7NSEvent
+__ZN7WebCore12EventHandler8keyEventERKNS_21PlatformKeyboardEventE
 __ZN7WebCore12EventHandler8keyEventEP7NSEvent
 __ZN7WebCore12EventHandler9mouseDownEP7NSEvent
 __ZN7WebCore12IconDatabase10setEnabledEb
index 72b6cc9f52223f3d0735437b76128cb2ca640aa7..c4dbba6b0f2f4227d90a445b9c1827ee9d088752 100644 (file)
@@ -1472,7 +1472,7 @@ bool EventHandler::keyEvent(const PlatformKeyboardEvent& initialKeyEvent)
     if (keypress->defaultHandled())
         return true;
     
-    if (handledByInputMethod)
+    if (handledByInputMethod || initialKeyEvent.isModifierKeyPress())
         return result;
     
     // If the default handling has been prevented on the keydown, we prevent it on
index 2368f6fc8e09ce5c7519e7be8b8b67b13b37ac5f..a344c0ad5e26dbe8c509b15cdbee381d69f1c26c 100644 (file)
@@ -73,6 +73,7 @@ namespace WebCore {
         bool ctrlKey() const { return m_ctrlKey; }
         bool altKey() const { return m_altKey; }
         bool metaKey() const { return m_metaKey; }
+        bool isModifierKeyPress() const { return m_isModifierKeyPress; }
 
         static bool currentCapsLockState();
 
@@ -110,6 +111,10 @@ namespace WebCore {
         bool m_ctrlKey;
         bool m_altKey;
         bool m_metaKey;
+        
+        // A control key event -- eg. keydown/up for shift, ctrl, alt, and meta -- needs
+        // a flag to indicate that we should not generate a keyPress event.
+        bool m_isModifierKeyPress;
 #if PLATFORM(WX)
         bool m_isWxCharEvent;
 #endif
index b96ac1b6c3f6bd76de5e02e4d28931f966beeebe..b905864dd7cb2f3443a1178c881174759c8d95d0 100644 (file)
@@ -480,6 +480,7 @@ PlatformKeyboardEvent::PlatformKeyboardEvent(GdkEventKey* event)
     , m_ctrlKey(event->state & GDK_CONTROL_MASK)
     , m_altKey(event->state & GDK_MOD1_MASK)
     , m_metaKey(event->state & GDK_MOD2_MASK)
+    , m_isModifierKeyPress(false)
 {
 }
 
index 71adc3275bedbbfac2be3ba434d3512c1f0e4d42..3e1733525286b993ff70281510e7b5b0ebc03817 100644 (file)
@@ -36,6 +36,32 @@ namespace WebCore {
 
 static String keyIdentifierForKeyEvent(NSEvent* event)
 {
+    if ([event type] == NSFlagsChanged) 
+        switch ([event keyCode]) {
+            case 54: // Right Command
+            case 55: // Left Command
+                return "Meta";
+                
+            case 57: // Capslock
+                return "CapsLock";
+                
+            case 56: // Left Shift
+            case 60: // Right Shift
+                return "Shift";
+                
+            case 58: // Left Alt
+            case 61: // Right Alt
+                return "Alt";
+                
+            case 59: // Left Ctrl
+            case 62: // Right Ctrl
+                return "Control";
+                
+            default:
+                ASSERT_NOT_REACHED();
+                return "";
+        }
+    
     NSString *s = [event charactersIgnoringModifiers];
     if ([s length] != 1) {
         LOG(Events, "received an unexpected number of characters in key event: %u", [s length]);
@@ -373,6 +399,33 @@ static int WindowsKeyCodeForKeyEvent(NSEvent* event)
         // VK_TAB (09) TAB key
         case 48: return 0x09;
 
+        // VK_APPS (5D) Right windows/meta key
+        case 54: // Right Command
+            return 0x5D;
+            
+        // VK_LWIN (5B) Left windows/meta key
+        case 55: // Left Command
+            return 0x5B;
+            
+        // VK_CAPITAL (14) caps locks key
+        case 57: // Capslock
+            return 0x14;
+            
+        // VK_SHIFT (10) either shift key
+        case 56: // Left Shift
+        case 60: // Right Shift
+            return 0x10;
+            
+        // VK_MENU (12) either alt key
+        case 58: // Left Alt
+        case 61: // Right Alt
+            return 0x12;
+            
+        // VK_CONTROL (11) either ctrl key
+        case 59: // Left Ctrl
+        case 62: // Right Ctrl
+            return 0x11;
+            
         // VK_CLEAR (0C) CLEAR key
         case 71: return 0x0C;
 
@@ -708,18 +761,64 @@ static int WindowsKeyCodeForKeyEvent(NSEvent* event)
     return 0;
 }
 
+static inline bool isKeyUpEvent(NSEvent *event)
+{
+    if ([event type] != NSFlagsChanged)
+        return false;
+    switch ([event keyCode]) {
+        case 54: // Right Command
+        case 55: // Left Command
+            return ([event modifierFlags] & NSCommandKeyMask) == 0;
+            
+        case 57: // Capslock
+            return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
+            
+        case 56: // Left Shift
+        case 60: // Right Shift
+            return ([event modifierFlags] & NSShiftKeyMask) == 0;
+            
+        case 58: // Left Alt
+        case 61: // Right Alt
+            return ([event modifierFlags] & NSAlternateKeyMask) == 0;
+            
+        case 59: // Left Ctrl
+        case 62: // Right Ctrl
+            return ([event modifierFlags] & NSControlKeyMask) == 0;
+            
+        case 63: // Function
+            return ([event modifierFlags] & NSFunctionKeyMask) == 0;
+    }
+    return false;
+}
+
+static inline String textFromEvent(NSEvent* event)
+{
+    if ([event type] == NSFlagsChanged)
+        return "";
+    return [event characters];
+}
+    
+    
+static inline String unmodifiedTextFromEvent(NSEvent* event)
+{
+    if ([event type] == NSFlagsChanged)
+        return "";
+    return [event charactersIgnoringModifiers];
+}
+    
 PlatformKeyboardEvent::PlatformKeyboardEvent(NSEvent *event, bool forceAutoRepeat)
-    : m_text([event characters])
-    , m_unmodifiedText([event charactersIgnoringModifiers])
+    : m_text(textFromEvent(event))
+    , m_unmodifiedText(unmodifiedTextFromEvent(event))
     , m_keyIdentifier(keyIdentifierForKeyEvent(event))
-    , m_isKeyUp([event type] == NSKeyUp)
-    , m_autoRepeat(forceAutoRepeat || [event isARepeat])
+    , m_isKeyUp([event type] == NSKeyUp || isKeyUpEvent(event))
+    , m_autoRepeat(([event type] != NSFlagsChanged) && (forceAutoRepeat || [event isARepeat]))
     , m_WindowsKeyCode(WindowsKeyCodeForKeyEvent(event))
     , m_isKeypad(isKeypadEvent(event))
     , m_shiftKey([event modifierFlags] & NSShiftKeyMask)
     , m_ctrlKey([event modifierFlags] & NSControlKeyMask)
     , m_altKey([event modifierFlags] & NSAlternateKeyMask)
     , m_metaKey([event modifierFlags] & NSCommandKeyMask)
+    , m_isModifierKeyPress([event type] == NSFlagsChanged)
     , m_macEvent(event)
 {
     // Turn 0x7F into 8, because backspace needs to always be 8.
index c1bb6acb08ebe40be5765154576cff9699cb1c42..63b3a7349092e8da035b2cf6925ee0904530bc16 100644 (file)
@@ -151,6 +151,7 @@ PlatformKeyboardEvent::PlatformKeyboardEvent(HWND, WPARAM virtualKeyCode, LPARAM
     , m_ctrlKey(GetKeyState(VK_CONTROL) & HIGH_BIT_MASK_SHORT)
     , m_altKey(GetKeyState(VK_MENU) & HIGH_BIT_MASK_SHORT)
     , m_metaKey(m_altKey)
+    , m_isModifierKeyPress(false)
 {
 }
 
index 41e616e172aa50ab44afb565fc0ec1b858ab2d80..f8f015573f9b2e9d40d39ed8351b85128ab027fe 100644 (file)
@@ -148,6 +148,7 @@ PlatformKeyboardEvent::PlatformKeyboardEvent(HWND hWnd, WPARAM wParam, LPARAM lP
     , m_ctrlKey(GetAsyncKeyState(VK_CONTROL) & HIGH_BIT_MASK_SHORT)
     , m_altKey(lParam & ALT_KEY_DOWN_MASK)
     , m_metaKey(lParam & ALT_KEY_DOWN_MASK) // FIXME: Is this right?
+    , m_isModifierKeyPress(false)
 {
     if (!m_shiftKey)
         m_text = String(singleCharacterString(tolower(wParam)));
index ee94dab85d4b7758fe9d71bda8db2830baa05336..1cbd60e65d4f49ab9c6118db6a2a31e94ad1da9c 100644 (file)
@@ -3585,6 +3585,13 @@ noPromisedData:
 {
     if (Frame* frame = core([self _frame]))
         frame->eventHandler()->capsLockStateMayHaveChanged();
+    
+    RetainPtr<WebHTMLView> selfProtector = self;
+    
+    //Don't make an event from the function key
+    if ([event keyCode] != 63)
+        core([self _frame])->eventHandler()->keyEvent(PlatformKeyboardEvent(event));
+        
     [super flagsChanged:event];
 }
 
@@ -5221,6 +5228,10 @@ static CGPoint coreGraphicsScreenPointForAppKitScreenPoint(NSPoint point)
         NSEvent *macEvent = platformEvent->macEvent();
         if ([macEvent type] == NSKeyDown && [_private->compController filterKeyDown:macEvent])
             return true;
+        
+        if ([macEvent type] == NSFlagsChanged)
+            return false;
+        
         parameters.event = event;
         _private->interpretKeyEventsParameters = &parameters;
         _private->receivedNOOP = NO;