From 50e98ffe7fd5fff5f7b0c6c9ed03b09df63458a4 Mon Sep 17 00:00:00 2001 From: ali kettab Date: Tue, 31 Oct 2023 22:16:14 +0100 Subject: [PATCH] Fix simulating keystrokes with modifier(s) under wxQt --- src/common/uiactioncmn.cpp | 6 ++++++ src/qt/uiaction.cpp | 28 +++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/common/uiactioncmn.cpp b/src/common/uiactioncmn.cpp index d0a9f522df..8921661fde 100644 --- a/src/common/uiactioncmn.cpp +++ b/src/common/uiactioncmn.cpp @@ -94,6 +94,7 @@ bool wxUIActionSimulatorImpl::MouseDragDrop(long x1, long y1, long x2, long y2, bool wxUIActionSimulator::Key(int keycode, int modifiers, bool isDown) { +#ifndef __WXQT__ wxASSERT_MSG( !(modifiers & wxMOD_META ), "wxMOD_META is not implemented" ); wxASSERT_MSG( !(modifiers & wxMOD_WIN ), @@ -108,6 +109,11 @@ wxUIActionSimulator::Key(int keycode, int modifiers, bool isDown) SimulateModifiers(modifiers, false); return rc; +#else + // Under wxQt we have to pass modifiers along with keycode for the simulation + // to succeed. + return m_impl->DoKey(keycode, modifiers, isDown); +#endif } void wxUIActionSimulator::SimulateModifiers(int modifiers, bool isDown) diff --git a/src/qt/uiaction.cpp b/src/qt/uiaction.cpp index 6321df3851..dc6d534b6e 100644 --- a/src/qt/uiaction.cpp +++ b/src/qt/uiaction.cpp @@ -164,16 +164,32 @@ static bool SimulateMouseButton( MouseAction mouseAction, return true; } -static bool SimulateKeyboardKey( KeyAction keyAction, Key key ) +static bool SimulateKeyboardKey( KeyAction keyAction, Key key, int modifiers ) { QWidget *widget = QApplication::focusWidget(); if ( !widget ) return false; + // I don't know if this is a bug in QTest or not, but simulating Shift+Char + // always produces a lowercase of that char! (which must be in uppercase) + if ( modifiers == wxMOD_SHIFT && key < 256 ) + { + const QChar qChar(key); + if ( qChar.isLetter() ) + { + widget->windowHandle() != nullptr ? + sendKeyEvent( keyAction, widget->windowHandle(), key, + QString(qChar.toUpper()), Qt::ShiftModifier ) : + sendKeyEvent( keyAction, widget, key, + QString(qChar.toUpper()), Qt::ShiftModifier ); + return true; + } + } + widget->windowHandle() != nullptr ? - keyEvent( keyAction, widget->windowHandle(), key ) : - keyEvent( keyAction, widget, key ); + keyEvent( keyAction, widget->windowHandle(), key, ConvertToQtModifiers(modifiers) ) : + keyEvent( keyAction, widget, key, ConvertToQtModifiers(modifiers) ); // If we found a widget then we successfully simulated an event: @@ -215,13 +231,11 @@ bool wxUIActionSimulatorQtImpl::DoKey(int keyCode, int modifiers, bool isDown) SaveModifierForMouseClicks(keyCode, isDown); Qt::KeyboardModifiers qtmodifiers; - enum Key key; - - key = (enum Key) wxQtConvertKeyCode( keyCode, modifiers, qtmodifiers ); + enum Key key = (enum Key) wxQtConvertKeyCode( keyCode, modifiers, qtmodifiers ); wxCHECK_MSG(key, false, wxT("No current key conversion equivalent in Qt")); KeyAction keyAction = isDown ? Press : Release; - return SimulateKeyboardKey( keyAction, key ); + return SimulateKeyboardKey( keyAction, key, modifiers ); }