From: Vadim Zeitlin Date: Sat, 11 Sep 2010 10:18:15 +0000 (+0000) Subject: Explicitly send events for modifier keys in wxUIActionSimulator. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/371412145fdcff75d46401eb7fcffdc047f195cb Explicitly send events for modifier keys in wxUIActionSimulator. Key down/up events for the modifiers were already explicitly sent under wxMSW and wxOSX but not under wxGTK where, as the result, the corresponding events were not generated at all. Do send these events explicitly to make the events generation consistent under all platforms now. This means that wxUIActionSimulator::DoKey() now generates exactly one event everywhere. Notice that the modifiers for the key events generated by the modifier keys are also the same under all platforms now which is not the case for the events actually generated by the user (wxMSW sets the corresponding bit for the modifier key down event but not the key up one while wxGTK does exactly the contrary). This should be fixed in the future so that wxUIActionSimulator generates the same sequence of events as the user would and that it's still the same for all platforms. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65516 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/uiaction.h b/include/wx/uiaction.h index e662b2c3c3..d681d5944a 100644 --- a/include/wx/uiaction.h +++ b/include/wx/uiaction.h @@ -70,11 +70,17 @@ private: // implementation level it makes more sense to have them in a single // function. // - // This is a simple wrapper verifying the input parameters validity around - // the platform-specific DoKey() method implemented in platform-specific - // files. + // It calls DoModifiers() to simulate pressing the modifier keys if + // necessary and then DoKey() for the key itself. bool Key(int keycode, int modifiers, bool isDown); + // Call DoKey() for all modifier keys whose bits are set in the parameter. + void SimulateModifiers(int modifier, bool isDown); + + + // The low-level port-specific function which really generates the key + // presses. It should generate exactly one key event with the given + // parameters. bool DoKey(int keycode, int modifiers, bool isDown); }; diff --git a/src/common/uiactioncmn.cpp b/src/common/uiactioncmn.cpp index a109097bd0..5afc7dae6a 100644 --- a/src/common/uiactioncmn.cpp +++ b/src/common/uiactioncmn.cpp @@ -57,10 +57,28 @@ wxUIActionSimulator::Key(int keycode, int modifiers, bool isDown) wxASSERT_MSG( !(modifiers & wxMOD_WIN ), "wxMOD_WIN is not implemented" ); - return DoKey(keycode, modifiers, isDown); + if ( isDown ) + SimulateModifiers(modifiers, true); + + bool rc = DoKey(keycode, modifiers, isDown); + + if ( !isDown ) + SimulateModifiers(modifiers, false); + + return rc; +} + +void wxUIActionSimulator::SimulateModifiers(int modifiers, bool isDown) +{ + if ( modifiers & wxMOD_SHIFT ) + DoKey(WXK_SHIFT, modifiers, isDown); + if ( modifiers & wxMOD_ALT ) + DoKey(WXK_ALT, modifiers, isDown); + if ( modifiers & wxMOD_CONTROL ) + DoKey(WXK_CONTROL, modifiers, isDown); } -bool wxUIActionSimulator::Char(int keycode, int modifiers) +bool wxUIActionSimulator::Char(int keycode, int modifiers) { Key(keycode, modifiers, true); Key(keycode, modifiers, false); diff --git a/src/msw/uiaction.cpp b/src/msw/uiaction.cpp index 293cf51078..d8f498cbd8 100644 --- a/src/msw/uiaction.cpp +++ b/src/msw/uiaction.cpp @@ -41,11 +41,6 @@ DWORD EventTypeForMouseButton(int button, bool isDown) } } -void DoSimulateKbdEvent(DWORD vk, bool isDown) -{ - keybd_event(vk, 0, isDown ? 0 : KEYEVENTF_KEYUP, 0); -} - } // anonymous namespace bool wxUIActionSimulator::MouseDown(int button) @@ -76,31 +71,12 @@ bool wxUIActionSimulator::MouseUp(int button) return true; } -bool wxUIActionSimulator::DoKey(int keycode, int modifiers, bool isDown) +bool +wxUIActionSimulator::DoKey(int keycode, int WXUNUSED(modifiers), bool isDown) { - if (isDown) - { - if (modifiers & wxMOD_SHIFT) - DoSimulateKbdEvent(VK_SHIFT, true); - if (modifiers & wxMOD_ALT) - DoSimulateKbdEvent(VK_MENU, true); - if (modifiers & wxMOD_CMD) - DoSimulateKbdEvent(VK_CONTROL, true); - } - DWORD vkkeycode = wxCharCodeWXToMSW(keycode); keybd_event(vkkeycode, 0, isDown ? 0 : KEYEVENTF_KEYUP, 0); - if (!isDown) - { - if (modifiers & wxMOD_SHIFT) - DoSimulateKbdEvent(VK_SHIFT, false); - if (modifiers & wxMOD_ALT) - DoSimulateKbdEvent(VK_MENU, false); - if (modifiers & wxMOD_CMD) - DoSimulateKbdEvent(VK_CONTROL, false); - } - return true; } diff --git a/src/osx/uiaction_osx.cpp b/src/osx/uiaction_osx.cpp index c2ebb321b3..2f8088f8c2 100644 --- a/src/osx/uiaction_osx.cpp +++ b/src/osx/uiaction_osx.cpp @@ -60,17 +60,6 @@ CGPoint GetMousePosition() return pos; } -bool SendCharCode(CGKeyCode keycode, bool isDown) -{ - wxCFRef - event(CGEventCreateKeyboardEvent(NULL, keycode, isDown)); - if ( !event ) - return false; - - CGEventPost(kCGHIDEventTap, event); - return true; -} - CGKeyCode wxCharCodeWXToOSX(wxKeyCode code) { CGKeyCode keycode; @@ -241,32 +230,18 @@ bool wxUIActionSimulator::MouseUp(int button) bool wxUIActionSimulator::DoKey(int keycode, int modifiers, bool isDown) { - if (isDown) - { - if (modifiers & wxMOD_SHIFT) - SendCharCode(kVK_Shift, true); - if (modifiers & wxMOD_ALT) - SendCharCode(kVK_Option, true); - if (modifiers & wxMOD_CMD) - SendCharCode(kVK_Command, true); - } - CGKeyCode cgcode = wxCharCodeWXToOSX((wxKeyCode)keycode); - if ( !SendCharCode(cgcode, isDown) ) - return false; - if(!isDown) - { - if (modifiers & wxMOD_SHIFT) - SendCharCode(kVK_Shift, false); - if (modifiers & wxMOD_ALT) - SendCharCode(kVK_Option, false); - if (modifiers & wxMOD_CMD) - SendCharCode(kVK_Command, false); - } + wxCFRef + event(CGEventCreateKeyboardEvent(NULL, cgcode, isDown)); + if ( !event ) + return false; + CGEventPost(kCGHIDEventTap, event); return true; } +} + #endif // wxUSE_UIACTIONSIMULATOR