Qt 4.8
qtestkeyboard.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #if !defined(QTESTKEYBOARD_H)
43 #define QTESTKEYBOARD_H
44 
45 #if 0
46 // inform syncqt
47 #pragma qt_no_master_include
48 #endif
49 
50 #include <QtTest/qtestassert.h>
51 #include <QtTest/qtest_global.h>
52 #include <QtTest/qtestsystem.h>
53 #include <QtTest/qtestspontaneevent.h>
54 
55 #include <QtCore/qpointer.h>
56 #include <QtGui/qapplication.h>
57 #include <QtGui/qevent.h>
58 #include <QtGui/qwidget.h>
59 
61 
63 
64 QT_MODULE(Test)
65 
66 namespace QTest
67 {
69 
70  static void simulateEvent(QWidget *widget, bool press, int code,
71  Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
72  {
73  QTEST_ASSERT(widget);
74  extern int Q_TESTLIB_EXPORT defaultKeyDelay();
75 
76  if (delay == -1 || delay < defaultKeyDelay())
77  delay = defaultKeyDelay();
78  if(delay > 0)
79  QTest::qWait(delay);
80 
81  QKeyEvent a(press ? QEvent::KeyPress : QEvent::KeyRelease, code, modifier, text, repeat);
83  if (!qApp->notify(widget, &a))
84  QTest::qWarn("Keyboard event not accepted by receiving widget");
85  }
86 
87  static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
88  QString text, Qt::KeyboardModifiers modifier, int delay=-1)
89  {
91 
92  if (!widget)
93  widget = QWidget::keyboardGrabber();
94  if (!widget) {
96  widget = apw->focusWidget() ? apw->focusWidget() : apw;
97  else
98  widget = QApplication::focusWidget();
99  }
100  if (!widget)
101  widget = QApplication::activeWindow();
102 
103  QTEST_ASSERT(widget);
104 
105  if (action == Click) {
106  QPointer<QWidget> ptr(widget);
107  sendKeyEvent(Press, widget, code, text, modifier, delay);
108  if (!ptr) {
109  // if we send key-events to embedded widgets, they might be destroyed
110  // when the user presses Return
111  return;
112  }
113  sendKeyEvent(Release, widget, code, text, modifier, delay);
114  return;
115  }
116 
117  bool repeat = false;
118 
119  if (action == Press) {
120  if (modifier & Qt::ShiftModifier)
121  simulateEvent(widget, true, Qt::Key_Shift, 0, QString(), false, delay);
122 
123  if (modifier & Qt::ControlModifier)
124  simulateEvent(widget, true, Qt::Key_Control, modifier & Qt::ShiftModifier, QString(), false, delay);
125 
126  if (modifier & Qt::AltModifier)
127  simulateEvent(widget, true, Qt::Key_Alt,
128  modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
129  if (modifier & Qt::MetaModifier)
130  simulateEvent(widget, true, Qt::Key_Meta, modifier & (Qt::ShiftModifier
131  | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
132  simulateEvent(widget, true, code, modifier, text, repeat, delay);
133  } else if (action == Release) {
134  simulateEvent(widget, false, code, modifier, text, repeat, delay);
135 
136  if (modifier & Qt::MetaModifier)
137  simulateEvent(widget, false, Qt::Key_Meta, modifier, QString(), false, delay);
138  if (modifier & Qt::AltModifier)
139  simulateEvent(widget, false, Qt::Key_Alt, modifier &
140  (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier), QString(), false, delay);
141 
142  if (modifier & Qt::ControlModifier)
143  simulateEvent(widget, false, Qt::Key_Control,
144  modifier & (Qt::ShiftModifier | Qt::ControlModifier), QString(), false, delay);
145 
146  if (modifier & Qt::ShiftModifier)
147  simulateEvent(widget, false, Qt::Key_Shift, modifier & Qt::ShiftModifier, QString(), false, delay);
148  }
149  }
150 
151  // Convenience function
152  static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code,
153  char ascii, Qt::KeyboardModifiers modifier, int delay=-1)
154  {
155  QString text;
156  if (ascii)
157  text = QString(QChar::fromLatin1(ascii));
158  sendKeyEvent(action, widget, code, text, modifier, delay);
159  }
160 
161  inline static void keyEvent(KeyAction action, QWidget *widget, char ascii,
162  Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
163  { sendKeyEvent(action, widget, asciiToKey(ascii), ascii, modifier, delay); }
164  inline static void keyEvent(KeyAction action, QWidget *widget, Qt::Key key,
165  Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
166  { sendKeyEvent(action, widget, key, keyToAscii(key), modifier, delay); }
167 
168  inline static void keyClicks(QWidget *widget, const QString &sequence,
169  Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
170  {
171  for (int i=0; i < sequence.length(); i++)
172  keyEvent(Click, widget, sequence.at(i).toLatin1(), modifier, delay);
173  }
174 
175  inline static void keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
176  { keyEvent(Press, widget, key, modifier, delay); }
177  inline static void keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
178  { keyEvent(Release, widget, key, modifier, delay); }
179  inline static void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
180  { keyEvent(Click, widget, key, modifier, delay); }
181  inline static void keyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
182  { keyEvent(Press, widget, key, modifier, delay); }
183  inline static void keyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
184  { keyEvent(Release, widget, key, modifier, delay); }
185  inline static void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
186  { keyEvent(Click, widget, key, modifier, delay); }
187 
188 }
189 
191 
193 
194 #endif // QTESTKEYBOARD_H
#define Q_TESTLIB_EXPORT
Definition: qtest_global.h:56
The QKeyEvent class describes a key event.
Definition: qevent.h:224
QWidget * focusWidget() const
Returns the last child of this widget that setFocus had been called on.
Definition: qwidget.cpp:6863
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
#define QT_MODULE(x)
Definition: qglobal.h:2783
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
static void keyEvent(KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
static void keyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
Simulates clicking a sequence of keys on a widget.
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or 0 if no application wi...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
long ASN1_INTEGER_get ASN1_INTEGER * a
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define qApp
static void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
static void sendKeyEvent(KeyAction action, QWidget *widget, Qt::Key code, QString text, Qt::KeyboardModifiers modifier, int delay=-1)
Definition: qtestkeyboard.h:87
static QChar fromLatin1(char c)
Converts the Latin-1 character c to its equivalent QChar.
Definition: qchar.h:378
const T * ptr(const T &t)
static void keyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
static QWidget * activePopupWidget()
Returns the active popup widget.
static void qWait(int ms)
Waits for ms milliseconds.
Definition: qtestsystem.h:62
Q_TESTLIB_EXPORT void qWarn(const char *message)
Definition: qtestcase.cpp:2102
static void keyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier=Qt::NoModifier, int delay=-1)
#define QTEST_ASSERT(cond)
Definition: qtestassert.h:53
int key
static QWidget * keyboardGrabber()
Returns the widget that is currently grabbing the keyboard input.
Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key)
Convert a Qt Key to an ascii char value.
Definition: qasciikey.cpp:245
char toLatin1() const
Returns the Latin-1 character equivalent to the QChar, or 0.
Definition: qchar.h:376
int Q_TESTLIB_EXPORT defaultKeyDelay()
Definition: qtestcase.cpp:1019
KeyAction
This enum describes possible actions for key handling.
Definition: qtestkeyboard.h:68
Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii)
Convert an ascii char key value to a Qt Key value.
Definition: qasciikey.cpp:57
#define QT_END_HEADER
Definition: qglobal.h:137
static void simulateEvent(QWidget *widget, bool press, int code, Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
Definition: qtestkeyboard.h:70
static QWidget * focusWidget()
Returns the application widget that has the keyboard input focus, or 0 if no widget in this applicati...
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...
#define text
Definition: qobjectdefs.h:80