Fix simulating keystrokes with modifier(s) under wxQt

This commit is contained in:
ali kettab 2023-10-31 22:16:14 +01:00
parent ed6f32e113
commit 50e98ffe7f
2 changed files with 27 additions and 7 deletions

View file

@ -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)

View file

@ -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 );
}