Bug 855975 part.6 Move nsWindow::OnKeyUp() to widget::NativeKey::HandleKeyUpMessage() r=jimm
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 29 May 2013 15:34:47 +0900
changeset 133233 cfef49bc5691deeedd8393c5fb2eb9d3b4cb7950
parent 133232 81aadfdcd59da90a6d922fb148a9bd81dd383e9d
child 133234 7b1704ec0e501fd01bfad3bfff557a8045fd1eeb
push id28671
push usermasayuki@d-toybox.com
push dateWed, 29 May 2013 06:35:17 +0000
treeherdermozilla-inbound@57461a161f93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs855975
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 855975 part.6 Move nsWindow::OnKeyUp() to widget::NativeKey::HandleKeyUpMessage() r=jimm
widget/windows/KeyboardLayout.cpp
widget/windows/KeyboardLayout.h
widget/windows/nsWindow.cpp
widget/windows/nsWindow.h
--- a/widget/windows/KeyboardLayout.cpp
+++ b/widget/windows/KeyboardLayout.cpp
@@ -383,41 +383,40 @@ VirtualKey::FillKbdState(PBYTE aKbdState
 
 /*****************************************************************************
  * mozilla::widget::NativeKey
  *****************************************************************************/
 
 NativeKey::NativeKey(nsWindowBase* aWidget,
                      const MSG& aKeyOrCharMessage,
                      const ModifierKeyState& aModKeyState) :
-  mWidget(aWidget), mDOMKeyCode(0), mMessage(aKeyOrCharMessage.message),
+  mWidget(aWidget), mMsg(aKeyOrCharMessage), mDOMKeyCode(0),
   mModKeyState(aModKeyState), mVirtualKeyCode(0), mOriginalVirtualKeyCode(0)
 {
   MOZ_ASSERT(aWidget);
   KeyboardLayout* keyboardLayout = KeyboardLayout::GetInstance();
   mKeyboardLayout = keyboardLayout->GetLayout();
-  mScanCode = WinUtils::GetScanCode(aKeyOrCharMessage.lParam);
-  mIsExtended = WinUtils::IsExtendedScanCode(aKeyOrCharMessage.lParam);
+  mScanCode = WinUtils::GetScanCode(mMsg.lParam);
+  mIsExtended = WinUtils::IsExtendedScanCode(mMsg.lParam);
   // On WinXP and WinServer2003, we cannot compute the virtual keycode for
   // extended keys due to the API limitation.
   bool canComputeVirtualKeyCodeFromScanCode =
     (!mIsExtended || WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION);
-  switch (mMessage) {
+  switch (mMsg.message) {
     case WM_KEYDOWN:
     case WM_KEYUP:
     case WM_SYSKEYDOWN:
     case WM_SYSKEYUP: {
       // First, resolve the IME converted virtual keycode to its original
       // keycode.
-      if (aKeyOrCharMessage.wParam == VK_PROCESSKEY) {
-        mOriginalVirtualKeyCode = static_cast<uint8_t>(
-          ::ImmGetVirtualKey(mWidget->GetWindowHandle()));
+      if (mMsg.wParam == VK_PROCESSKEY) {
+        mOriginalVirtualKeyCode =
+          static_cast<uint8_t>(::ImmGetVirtualKey(mMsg.hwnd));
       } else {
-        mOriginalVirtualKeyCode =
-          static_cast<uint8_t>(aKeyOrCharMessage.wParam);
+        mOriginalVirtualKeyCode = static_cast<uint8_t>(mMsg.wParam);
       }
 
       // Most keys are not distinguished as left or right keys.
       bool isLeftRightDistinguishedKey = false;
 
       // mOriginalVirtualKeyCode must not distinguish left or right of
       // Shift, Control or Alt.
       switch (mOriginalVirtualKeyCode) {
@@ -659,16 +658,37 @@ NativeKey::ComputeUnicharFromScanCode() 
 }
 
 void
 NativeKey::InitKeyEvent(nsKeyEvent& aKeyEvent,
                         const ModifierKeyState& aModKeyState) const
 {
   nsIntPoint point(0, 0);
   mWidget->InitEvent(aKeyEvent, &point);
+
+  switch (aKeyEvent.message) {
+    case NS_KEY_DOWN:
+      break;
+    case NS_KEY_UP:
+      aKeyEvent.keyCode = mDOMKeyCode;
+      // Set defaultPrevented of the key event if the VK_MENU is not a system
+      // key release, so that the menu bar does not trigger.  This helps avoid
+      // triggering the menu bar for ALT key accelerators used in assistive
+      // technologies such as Window-Eyes and ZoomText or for switching open
+      // state of IME.
+      aKeyEvent.mFlags.mDefaultPrevented =
+        (mOriginalVirtualKeyCode == VK_MENU && mMsg.message != WM_SYSKEYUP);
+      break;
+    case NS_KEY_PRESS:
+      break;
+    default:
+      MOZ_NOT_REACHED("Invalid event message");
+      break;
+  }
+
   aKeyEvent.mKeyNameIndex = mKeyNameIndex;
   aKeyEvent.location = GetKeyLocation();
   aModKeyState.InitInputEvent(aKeyEvent);
 }
 
 bool
 NativeKey::DispatchKeyEvent(nsKeyEvent& aKeyEvent,
                             const MSG* aMsgSentToPlugin) const
@@ -682,16 +702,39 @@ NativeKey::DispatchKeyEvent(nsKeyEvent& 
     pluginEvent.wParam = aMsgSentToPlugin->wParam;
     pluginEvent.lParam = aMsgSentToPlugin->lParam;
     aKeyEvent.pluginEvent = static_cast<void*>(&pluginEvent);
   }
 
   return mWidget->DispatchWindowEvent(&aKeyEvent);
 }
 
+bool
+NativeKey::HandleKeyUpMessage(bool* aEventDispatched) const
+{
+  MOZ_ASSERT(mMsg.message == WM_KEYUP || mMsg.message == WM_SYSKEYUP);
+
+  if (aEventDispatched) {
+    *aEventDispatched = false;
+  }
+
+  // Ignore [shift+]alt+space so the OS can handle it.
+  if (mModKeyState.IsAlt() && !mModKeyState.IsControl() &&
+      mVirtualKeyCode == VK_SPACE) {
+    return false;
+  }
+
+  nsKeyEvent keyupEvent(true, NS_KEY_UP, mWidget);
+  InitKeyEvent(keyupEvent, mModKeyState);
+  if (aEventDispatched) {
+    *aEventDispatched = true;
+  }
+  return DispatchKeyEvent(keyupEvent, &mMsg);
+}
+
 /*****************************************************************************
  * mozilla::widget::KeyboardLayout
  *****************************************************************************/
 
 KeyboardLayout* KeyboardLayout::sInstance = nullptr;
 nsIIdleServiceInternal* KeyboardLayout::sIdleService = nullptr;
 
 // static
--- a/widget/windows/KeyboardLayout.h
+++ b/widget/windows/KeyboardLayout.h
@@ -285,20 +285,20 @@ public:
 
   uint32_t GetDOMKeyCode() const { return mDOMKeyCode; }
   KeyNameIndex GetKeyNameIndex() const { return mKeyNameIndex; }
   const UniCharsAndModifiers& GetCommittedCharsAndModifiers() const
   {
     return mCommittedCharsAndModifiers;
   }
 
-  UINT GetMessage() const { return mMessage; }
+  UINT GetMessage() const { return mMsg.message; }
   bool IsKeyDownMessage() const
   {
-    return (mMessage == WM_KEYDOWN || mMessage == WM_SYSKEYDOWN);
+    return (mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
   }
   WORD GetScanCode() const { return mScanCode; }
   uint8_t GetVirtualKeyCode() const { return mVirtualKeyCode; }
   uint8_t GetOriginalVirtualKeyCode() const { return mOriginalVirtualKeyCode; }
 
   /**
    * Wraps MapVirtualKeyEx() with MAPVK_VSC_TO_VK.
    */
@@ -326,25 +326,30 @@ public:
 
   /**
    * Dispatches the key event.  Returns true if the event is consumed.
    * Otherwise, false.
    */
   bool DispatchKeyEvent(nsKeyEvent& aKeyEvent,
                         const MSG* aMsgSentToPlugin = nullptr) const;
 
+  /**
+   * Handles keyup message.  Returns true if the event is consumed.
+   * Otherwise, false.
+   */
+  bool HandleKeyUpMessage(bool* aEventDispatched = nullptr) const;
+
 private:
   nsRefPtr<nsWindowBase> mWidget;
   HKL mKeyboardLayout;
+  MSG mMsg;
+
   uint32_t mDOMKeyCode;
   KeyNameIndex mKeyNameIndex;
 
-  // The message which the instance was initialized with.
-  UINT mMessage;
-
   ModifierKeyState mModKeyState;
 
   // mVirtualKeyCode distinguishes left key or right key of modifier key.
   uint8_t mVirtualKeyCode;
   // mOriginalVirtualKeyCode doesn't distinguish left key or right key of
   // modifier key.  However, if the given keycode is VK_PROCESS, it's resolved
   // to a keycode before it's handled by IME.
   uint8_t mOriginalVirtualKeyCode;
--- a/widget/windows/nsWindow.cpp
+++ b/widget/windows/nsWindow.cpp
@@ -5629,44 +5629,23 @@ LRESULT nsWindow::ProcessCharMessage(con
   // if a child window didn't handle it (for example Alt+Space in a content window)
   ModifierKeyState modKeyState;
   NativeKey nativeKey(this, aMsg, modKeyState);
   return OnChar(aMsg, nativeKey, modKeyState, aEventDispatched);
 }
 
 LRESULT nsWindow::ProcessKeyUpMessage(const MSG &aMsg, bool *aEventDispatched)
 {
-  NS_PRECONDITION(aMsg.message == WM_KEYUP || aMsg.message == WM_SYSKEYUP,
-                  "message is not keydown event");
-  PR_LOG(gWindowsLog, PR_LOG_ALWAYS,
-         ("%s VK=%d\n", aMsg.message == WM_SYSKEYDOWN ?
-                        "WM_SYSKEYUP" : "WM_KEYUP", aMsg.wParam));
+  if (IMEHandler::IsComposingOn(this)) {
+    return 0;
+  }
 
   ModifierKeyState modKeyState;
-
-  // Note: the original code passed (HIWORD(lParam)) to OnKeyUp as
-  // scan code. However, this breaks Alt+Num pad input.
-  // MSDN states the following:
-  //  Typically, ToAscii performs the translation based on the
-  //  virtual-key code. In some cases, however, bit 15 of the
-  //  uScanCode parameter may be used to distinguish between a key
-  //  press and a key release. The scan code is used for
-  //  translating ALT+number key combinations.
-
-  // ignore [shift+]alt+space so the OS can handle it
-  if (modKeyState.IsAlt() && !modKeyState.IsControl() &&
-      IS_VK_DOWN(NS_VK_SPACE)) {
-    return FALSE;
-  }
-
-  if (!IMEHandler::IsComposingOn(this)) {
-    return OnKeyUp(aMsg, modKeyState, aEventDispatched);
-  }
-
-  return 0;
+  NativeKey nativeKey(this, aMsg, modKeyState);
+  return static_cast<LRESULT>(nativeKey.HandleKeyUpMessage(aEventDispatched));
 }
 
 LRESULT nsWindow::ProcessKeyDownMessage(const MSG &aMsg,
                                         bool *aEventDispatched)
 {
   PR_LOG(gWindowsLog, PR_LOG_ALWAYS,
          ("%s VK=%d\n", aMsg.message == WM_SYSKEYDOWN ?
                         "WM_SYSKEYDOWN" : "WM_KEYDOWN", aMsg.wParam));
@@ -5885,17 +5864,18 @@ nsWindow::SynthesizeNativeKeyEvent(int32
       argumentKeySpecific ? argumentKeySpecific : aNativeKeyCode);
     LPARAM lParam = static_cast<LPARAM>(scanCode << 16);
     // Add extended key flag to the lParam for right control key and right alt
     // key.
     if (keySpecific == VK_RCONTROL || keySpecific == VK_RMENU) {
       lParam |= 0x1000000;
     }
     MSG msg = WinUtils::InitMSG(WM_KEYUP, key, lParam);
-    OnKeyUp(msg, modKeyState, nullptr);
+    NativeKey nativeKey(this, msg, modKeyState);
+    nativeKey.HandleKeyUpMessage();
   }
 
   // Restore old key state and layout
   ::SetKeyboardState(originalKbdState);
   keyboardLayout->RestoreLayout();
 
   // Don't unload the layout if it's installed actually.
   for (uint32_t i = 0; i < keyboardLayoutListCount; i++) {
@@ -6775,41 +6755,16 @@ LRESULT nsWindow::OnKeyDown(const MSG &a
     keypressEvent.keyCode = DOMKeyCode;
     nativeKey.InitKeyEvent(keypressEvent, aModKeyState);
     nativeKey.DispatchKeyEvent(keypressEvent);
   }
 
   return noDefault;
 }
 
-// OnKeyUp
-LRESULT nsWindow::OnKeyUp(const MSG &aMsg,
-                          const ModifierKeyState &aModKeyState,
-                          bool *aEventDispatched)
-{
-  // NOTE: VK_PROCESSKEY never comes with WM_KEYUP
-  PR_LOG(gWindowsLog, PR_LOG_ALWAYS,
-         ("nsWindow::OnKeyUp wParam(VK)=%d\n", aMsg.wParam));
-
-  if (aEventDispatched)
-    *aEventDispatched = true;
-  nsKeyEvent keyupEvent(true, NS_KEY_UP, this);
-  NativeKey nativeKey(this, aMsg, aModKeyState);
-  keyupEvent.keyCode = nativeKey.GetDOMKeyCode();
-  nativeKey.InitKeyEvent(keyupEvent);
-  // Set defaultPrevented of the key event if the VK_MENU is not a system key
-  // release, so that the menu bar does not trigger.  This helps avoid
-  // triggering the menu bar for ALT key accelerators used in assistive
-  // technologies such as Window-Eyes and ZoomText or for switching open state
-  // of IME.
-  keyupEvent.mFlags.mDefaultPrevented =
-    (aMsg.wParam == VK_MENU && aMsg.message != WM_SYSKEYUP);
-  return nativeKey.DispatchKeyEvent(keyupEvent, &aMsg);
-}
-
 // OnChar
 LRESULT nsWindow::OnChar(const MSG &aMsg,
                          const NativeKey& aNativeKey,
                          const ModifierKeyState &aModKeyState,
                          bool *aEventDispatched,
                          const EventFlags *aExtraFlags)
 {
   // ignore [shift+]alt+space so the OS can handle it
--- a/widget/windows/nsWindow.h
+++ b/widget/windows/nsWindow.h
@@ -372,19 +372,16 @@ protected:
                                  const NativeKey& aNativeKey,
                                  const mozilla::widget::ModifierKeyState &aModKeyState,
                                  bool *aEventDispatched,
                                  const mozilla::widget::EventFlags* aExtraFlags = nullptr);
   LRESULT                 OnKeyDown(const MSG &aMsg,
                                     const mozilla::widget::ModifierKeyState &aModKeyState,
                                     bool *aEventDispatched,
                                     nsFakeCharMessage* aFakeCharMessage);
-  LRESULT                 OnKeyUp(const MSG &aMsg,
-                                  const mozilla::widget::ModifierKeyState &aModKeyState,
-                                  bool *aEventDispatched);
   bool                    OnGesture(WPARAM wParam, LPARAM lParam);
   bool                    OnTouch(WPARAM wParam, LPARAM lParam);
   bool                    OnHotKey(WPARAM wParam, LPARAM lParam);
   bool                    OnPaint(HDC aDC, uint32_t aNestingLevel);
   void                    OnWindowPosChanged(WINDOWPOS *wp, bool& aResult);
   void                    OnWindowPosChanging(LPWINDOWPOS& info);
   void                    OnSysColorChanged();