| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtTest module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QTESTKEYBOARD_H |
| 41 | #define QTESTKEYBOARD_H |
| 42 | |
| 43 | #if 0 |
| 44 | // inform syncqt |
| 45 | #pragma qt_no_master_include |
| 46 | #endif |
| 47 | |
| 48 | #include <QtTest/qtestassert.h> |
| 49 | #include <QtTest/qttestglobal.h> |
| 50 | #include <QtTest/qtestsystem.h> |
| 51 | #include <QtTest/qtestspontaneevent.h> |
| 52 | |
| 53 | #include <QtCore/qpointer.h> |
| 54 | #include <QtGui/qguiapplication.h> |
| 55 | #include <QtGui/qwindow.h> |
| 56 | #include <QtGui/qevent.h> |
| 57 | #if QT_CONFIG(shortcut) |
| 58 | # include <QtGui/qkeysequence.h> |
| 59 | #endif |
| 60 | |
| 61 | #ifdef QT_WIDGETS_LIB |
| 62 | #include <QtWidgets/qwidget.h> |
| 63 | #include <QtWidgets/qapplication.h> |
| 64 | #endif |
| 65 | |
| 66 | QT_BEGIN_NAMESPACE |
| 67 | |
| 68 | Q_GUI_EXPORT void qt_handleKeyEvent(QWindow *w, QEvent::Type t, int k, Qt::KeyboardModifiers mods, const QString & text = QString(), bool autorep = false, ushort count = 1); |
| 69 | Q_GUI_EXPORT bool qt_sendShortcutOverrideEvent(QObject *o, ulong timestamp, int k, Qt::KeyboardModifiers mods, const QString &text = QString(), bool autorep = false, ushort count = 1); |
| 70 | |
| 71 | namespace QTest |
| 72 | { |
| 73 | enum KeyAction { Press, Release, Click, Shortcut }; |
| 74 | |
| 75 | static void simulateEvent(QWindow *window, bool press, int code, |
| 76 | Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1) |
| 77 | { |
| 78 | QEvent::Type type; |
| 79 | type = press ? QEvent::KeyPress : QEvent::KeyRelease; |
| 80 | qt_handleKeyEvent(window, type, code, modifier, text, repeat, delay); |
| 81 | qApp->processEvents(); |
| 82 | } |
| 83 | |
| 84 | static void sendKeyEvent(KeyAction action, QWindow *window, Qt::Key code, |
| 85 | QString text, Qt::KeyboardModifiers modifier, int delay=-1) |
| 86 | { |
| 87 | QTEST_ASSERT(qApp); |
| 88 | |
| 89 | if (!window) |
| 90 | window = QGuiApplication::focusWindow(); |
| 91 | |
| 92 | QTEST_ASSERT(window); |
| 93 | |
| 94 | |
| 95 | if (action == Click) { |
| 96 | sendKeyEvent(Press, window, code, text, modifier, delay); |
| 97 | sendKeyEvent(Release, window, code, text, modifier, delay); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | bool repeat = false; |
| 102 | |
| 103 | if (action == Shortcut) { |
| 104 | int timestamp = 0; |
| 105 | qt_sendShortcutOverrideEvent(window, timestamp, code, modifier, text, repeat); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if (action == Press) { |
| 110 | if (modifier & Qt::ShiftModifier) |
| 111 | simulateEvent(window, true, Qt::Key_Shift, Qt::KeyboardModifiers(), QString(), false, delay); |
| 112 | |
| 113 | if (modifier & Qt::ControlModifier) |
| 114 | simulateEvent(window, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay); |
| 115 | |
| 116 | if (modifier & Qt::AltModifier) |
| 117 | simulateEvent(window, true, Qt::Key_Alt, |
| 118 | modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay); |
| 119 | if (modifier & Qt::MetaModifier) |
| 120 | simulateEvent(window, true, Qt::Key_Meta, modifier & (Qt::ShiftModifier |
| 121 | | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay); |
| 122 | simulateEvent(window, true, code, modifier, text, repeat, delay); |
| 123 | } else if (action == Release) { |
| 124 | simulateEvent(window, false, code, modifier, text, repeat, delay); |
| 125 | |
| 126 | if (modifier & Qt::MetaModifier) |
| 127 | simulateEvent(window, false, Qt::Key_Meta, modifier, QString(), false, delay); |
| 128 | if (modifier & Qt::AltModifier) |
| 129 | simulateEvent(window, false, Qt::Key_Alt, modifier & |
| 130 | (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay); |
| 131 | |
| 132 | if (modifier & Qt::ControlModifier) |
| 133 | simulateEvent(window, false, Qt::Key_Control, |
| 134 | modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay); |
| 135 | |
| 136 | if (modifier & Qt::ShiftModifier) |
| 137 | simulateEvent(window, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Convenience function |
| 142 | static void sendKeyEvent(KeyAction action, QWindow *window, Qt::Key code, |
| 143 | char ascii, Qt::KeyboardModifiers modifier, int delay=-1) |
| 144 | { |
| 145 | QString text; |
| 146 | if (ascii) |
| 147 | text = QString(QChar::fromLatin1(ascii)); |
| 148 | sendKeyEvent(action, window, code, text, modifier, delay); |
| 149 | } |
| 150 | |
| 151 | inline static void keyEvent(KeyAction action, QWindow *window, char ascii, |
| 152 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 153 | { sendKeyEvent(action, window, asciiToKey(ascii), ascii, modifier, delay); } |
| 154 | inline static void keyEvent(KeyAction action, QWindow *window, Qt::Key key, |
| 155 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 156 | { sendKeyEvent(action, window, key, keyToAscii(key), modifier, delay); } |
| 157 | |
| 158 | [[maybe_unused]] inline static void keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 159 | { keyEvent(Click, window, key, modifier, delay); } |
| 160 | [[maybe_unused]] inline static void keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 161 | { keyEvent(Click, window, key, modifier, delay); } |
| 162 | [[maybe_unused]] inline static void keyRelease(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 163 | { keyEvent(Release, window, key, modifier, delay); } |
| 164 | [[maybe_unused]] inline static void keyRelease(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 165 | { keyEvent(Release, window, key, modifier, delay); } |
| 166 | [[maybe_unused]] inline static void keyPress(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 167 | { keyEvent(Press, window, key, modifier, delay); } |
| 168 | [[maybe_unused]] inline static void keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 169 | { keyEvent(Press, window, key, modifier, delay); } |
| 170 | |
| 171 | #if QT_CONFIG(shortcut) |
| 172 | [[maybe_unused]] inline static void keySequence(QWindow *window, const QKeySequence &keySequence) |
| 173 | { |
| 174 | for (int i = 0; i < keySequence.count(); ++i) { |
| 175 | const Qt::Key key = keySequence[i].key(); |
| 176 | const Qt::KeyboardModifiers modifiers = keySequence[i].keyboardModifiers(); |
| 177 | keyClick(window, key, modifiers); |
| 178 | } |
| 179 | } |
| 180 | #endif |
| 181 | |
| 182 | #ifdef QT_WIDGETS_LIB |
| 183 | static void simulateEvent(QWidget *widget, bool press, int code, |
| 184 | Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1) |
| 185 | { |
| 186 | QTEST_ASSERT(widget); |
| 187 | extern int Q_TESTLIB_EXPORT defaultKeyDelay(); |
| 188 | |
| 189 | if (delay == -1 || delay < defaultKeyDelay()) |
| 190 | delay = defaultKeyDelay(); |
| 191 | if (delay > 0) |
| 192 | QTest::qWait(delay); |
| 193 | |
| 194 | QKeyEvent a(press ? QEvent::KeyPress : QEvent::KeyRelease, code, modifier, text, repeat); |
| 195 | QSpontaneKeyEvent::setSpontaneous(&a); |
| 196 | |
| 197 | if (press && qt_sendShortcutOverrideEvent(widget, a.timestamp(), code, modifier, text, repeat)) |
| 198 | return; |
| 199 | if (!qApp->notify(widget, &a)) |
| 200 | QTest::qWarn("Keyboard event not accepted by receiving widget" ); |
| 201 | } |
| 202 | |
| 203 | static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code, |
| 204 | QString text, Qt::KeyboardModifiers modifier, int delay=-1) |
| 205 | { |
| 206 | QTEST_ASSERT(qApp); |
| 207 | |
| 208 | if (!widget) |
| 209 | widget = QWidget::keyboardGrabber(); |
| 210 | if (!widget) { |
| 211 | // Popup widgets stealthily steal the keyboard grab |
| 212 | if (QWidget *apw = QApplication::activePopupWidget()) |
| 213 | widget = apw->focusWidget() ? apw->focusWidget() : apw; |
| 214 | } |
| 215 | if (!widget) { |
| 216 | QWindow *window = QGuiApplication::focusWindow(); |
| 217 | if (window) { |
| 218 | sendKeyEvent(action, window, code, text, modifier, delay); |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | if (!widget) |
| 223 | widget = QApplication::focusWidget(); |
| 224 | if (!widget) |
| 225 | widget = QApplication::activeWindow(); |
| 226 | |
| 227 | QTEST_ASSERT(widget); |
| 228 | |
| 229 | if (action == Click) { |
| 230 | QPointer<QWidget> ptr(widget); |
| 231 | sendKeyEvent(Press, widget, code, text, modifier, delay); |
| 232 | if (!ptr) { |
| 233 | // if we send key-events to embedded widgets, they might be destroyed |
| 234 | // when the user presses Return |
| 235 | return; |
| 236 | } |
| 237 | sendKeyEvent(Release, widget, code, text, modifier, delay); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | bool repeat = false; |
| 242 | |
| 243 | if (action == Press) { |
| 244 | if (modifier & Qt::ShiftModifier) |
| 245 | simulateEvent(widget, true, Qt::Key_Shift, Qt::KeyboardModifiers(), QString(), false, delay); |
| 246 | |
| 247 | if (modifier & Qt::ControlModifier) |
| 248 | simulateEvent(widget, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay); |
| 249 | |
| 250 | if (modifier & Qt::AltModifier) |
| 251 | simulateEvent(widget, true, Qt::Key_Alt, |
| 252 | modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay); |
| 253 | if (modifier & Qt::MetaModifier) |
| 254 | simulateEvent(widget, true, Qt::Key_Meta, modifier & (Qt::ShiftModifier |
| 255 | | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay); |
| 256 | simulateEvent(widget, true, code, modifier, text, repeat, delay); |
| 257 | } else if (action == Release) { |
| 258 | simulateEvent(widget, false, code, modifier, text, repeat, delay); |
| 259 | |
| 260 | if (modifier & Qt::MetaModifier) |
| 261 | simulateEvent(widget, false, Qt::Key_Meta, modifier, QString(), false, delay); |
| 262 | if (modifier & Qt::AltModifier) |
| 263 | simulateEvent(widget, false, Qt::Key_Alt, modifier & |
| 264 | (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay); |
| 265 | |
| 266 | if (modifier & Qt::ControlModifier) |
| 267 | simulateEvent(widget, false, Qt::Key_Control, |
| 268 | modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay); |
| 269 | |
| 270 | if (modifier & Qt::ShiftModifier) |
| 271 | simulateEvent(widget, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Convenience function |
| 276 | static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code, |
| 277 | char ascii, Qt::KeyboardModifiers modifier, int delay=-1) |
| 278 | { |
| 279 | QString text; |
| 280 | if (ascii) |
| 281 | text = QString(QChar::fromLatin1(ascii)); |
| 282 | sendKeyEvent(action, widget, code, text, modifier, delay); |
| 283 | } |
| 284 | |
| 285 | inline static void keyEvent(KeyAction action, QWidget *widget, char ascii, |
| 286 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 287 | { sendKeyEvent(action, widget, asciiToKey(ascii), ascii, modifier, delay); } |
| 288 | inline static void keyEvent(KeyAction action, QWidget *widget, Qt::Key key, |
| 289 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 290 | { sendKeyEvent(action, widget, key, keyToAscii(key), modifier, delay); } |
| 291 | |
| 292 | inline static void keyClicks(QWidget *widget, const QString &sequence, |
| 293 | Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 294 | { |
| 295 | for (int i=0; i < sequence.length(); i++) |
| 296 | keyEvent(Click, widget, sequence.at(i).toLatin1(), modifier, delay); |
| 297 | } |
| 298 | |
| 299 | inline static void keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 300 | { keyEvent(Press, widget, key, modifier, delay); } |
| 301 | inline static void keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 302 | { keyEvent(Release, widget, key, modifier, delay); } |
| 303 | inline static void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 304 | { keyEvent(Click, widget, key, modifier, delay); } |
| 305 | inline static void keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 306 | { keyEvent(Press, widget, key, modifier, delay); } |
| 307 | inline static void keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 308 | { keyEvent(Release, widget, key, modifier, delay); } |
| 309 | inline static void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) |
| 310 | { keyEvent(Click, widget, key, modifier, delay); } |
| 311 | |
| 312 | #if QT_CONFIG(shortcut) |
| 313 | inline static void keySequence(QWidget *widget, const QKeySequence &keySequence) |
| 314 | { |
| 315 | for (int i = 0; i < keySequence.count(); ++i) { |
| 316 | const Qt::Key key = keySequence[i].key(); |
| 317 | const Qt::KeyboardModifiers modifiers = keySequence[i].keyboardModifiers(); |
| 318 | keyClick(widget, key, modifiers); |
| 319 | } |
| 320 | } |
| 321 | #endif |
| 322 | |
| 323 | #endif // QT_WIDGETS_LIB |
| 324 | |
| 325 | } |
| 326 | |
| 327 | QT_END_NAMESPACE |
| 328 | |
| 329 | #endif // QTESTKEYBOARD_H |
| 330 | |