Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickevents.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include <QtCore/qmap.h>
7#include <QtGui/private/qguiapplication_p.h>
8#include <QtGui/private/qinputdevice_p.h>
9#include <QtGui/private/qevent_p.h>
10#include <QtQuick/private/qquickitem_p.h>
11#include <QtQuick/private/qquickpointerhandler_p.h>
12#include <QtQuick/private/qquickpointerhandler_p_p.h>
13#include <QtQuick/private/qquickwindow_p.h>
14#include <private/qdebug_p.h>
15
17
18Q_LOGGING_CATEGORY(lcPointerEvents, "qt.quick.pointer.events")
19
20/*!
21 \qmltype KeyEvent
22 \nativetype QQuickKeyEvent
23 \inqmlmodule QtQuick
24 \ingroup qtquick-input-events
25
26 \brief Provides information about a key event.
27
28 For example, the following changes the Item's state property when the Enter
29 key is pressed:
30 \qml
31Item {
32 focus: true
33 Keys.onPressed: (event)=> { if (event.key == Qt.Key_Enter) state = 'ShowDetails'; }
34}
35 \endqml
36*/
37
38/*!
39 \qmlproperty int QtQuick::KeyEvent::key
40
41 This property holds the code of the key that was pressed or released.
42
43 See \l {Qt::Key}{Qt.Key} for the list of keyboard codes. These codes are
44 independent of the underlying window system. Note that this
45 function does not distinguish between capital and non-capital
46 letters; use the \l {KeyEvent::}{text} property for this purpose.
47
48 A value of either 0 or \l {Qt::Key_unknown}{Qt.Key_Unknown} means that the event is not
49 the result of a known key; for example, it may be the result of
50 a compose sequence, a keyboard macro, or due to key event
51 compression.
52*/
53
54/*!
55 \qmlproperty string QtQuick::KeyEvent::text
56
57 This property holds the Unicode text that the key generated.
58 The text returned can be an empty string in cases where modifier keys,
59 such as Shift, Control, Alt, and Meta, are being pressed or released.
60 In such cases \c key will contain a valid value
61*/
62
63/*!
64 \qmlproperty bool QtQuick::KeyEvent::isAutoRepeat
65
66 This property holds whether this event comes from an auto-repeating key.
67*/
68
69/*!
70 \qmlproperty quint32 QtQuick::KeyEvent::nativeScanCode
71
72 This property contains the native scan code of the key that was pressed. It is
73 passed through from QKeyEvent unchanged.
74
75 \sa QKeyEvent::nativeScanCode()
76*/
77
78/*!
79 \qmlproperty int QtQuick::KeyEvent::count
80
81 This property holds the number of keys involved in this event. If \l KeyEvent::text
82 is not empty, this is simply the length of the string.
83*/
84
85/*!
86 \qmlproperty bool QtQuick::KeyEvent::accepted
87
88 Setting \a accepted to true prevents the key event from being
89 propagated to the item's parent.
90
91 Generally, if the item acts on the key event then it should be accepted
92 so that ancestor items do not also respond to the same event.
93*/
94
95/*!
96 \qmlproperty int QtQuick::KeyEvent::modifiers
97
98 This property holds the keyboard modifier flags that existed immediately
99 before the event occurred.
100
101 It contains a bitwise combination of numeric values (the same as in Qt::KeyboardModifier):
102
103 \value Qt.NoModifier No modifier key is pressed.
104 \value Qt.ShiftModifier A Shift key on the keyboard is pressed.
105 \value Qt.ControlModifier A Ctrl key on the keyboard is pressed.
106 \value Qt.AltModifier An Alt key on the keyboard is pressed.
107 \value Qt.MetaModifier A Meta key on the keyboard is pressed.
108 \value Qt.KeypadModifier A keypad button is pressed.
109 \value Qt.GroupSwitchModifier X11 only. A Mode_switch key on the keyboard is pressed.
110
111 For example, to react to a Shift key + Enter key combination:
112 \qml
113 Item {
114 focus: true
115 Keys.onPressed: (event)=> {
116 if ((event.key == Qt.Key_Enter) && (event.modifiers & Qt.ShiftModifier))
117 doSomething();
118 }
119 }
120 \endqml
121*/
122
123/*!
124 \qmlmethod bool QtQuick::KeyEvent::matches(StandardKey matchKey)
125 \since 5.2
126
127 Returns \c true if the key event matches the given standard \a matchKey; otherwise returns \c false.
128
129 \qml
130 Item {
131 focus: true
132 Keys.onPressed: (event)=> {
133 if (event.matches(StandardKey.Undo))
134 myModel.undo();
135 else if (event.matches(StandardKey.Redo))
136 myModel.redo();
137 }
138 }
139 \endqml
140
141 \sa QKeySequence::StandardKey
142*/
143#if QT_CONFIG(shortcut)
144bool QQuickKeyEvent::matches(QKeySequence::StandardKey matchKey) const
145{
146 // copying QKeyEvent::matches
147 uint searchkey = (modifiers() | key()) & ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
148
149 const QList<QKeySequence> bindings = QKeySequence::keyBindings(matchKey);
150 return bindings.contains(QKeySequence(searchkey));
151}
152#endif
153
154
155/*!
156 \qmltype MouseEvent
157 \nativetype QQuickMouseEvent
158 \inqmlmodule QtQuick
159 \ingroup qtquick-input-events
160
161 \brief Provides information about a mouse event.
162
163 The position of the mouse can be found via the \l {Item::x} {x} and \l {Item::y} {y} properties.
164 The button that caused the event is available via the \l button property.
165
166 \sa MouseArea
167*/
168
169/*!
170 \internal
171 \class QQuickMouseEvent
172*/
173
174/*!
175 \qmlproperty real QtQuick::MouseEvent::x
176 \qmlproperty real QtQuick::MouseEvent::y
177
178 These properties hold the coordinates of the position supplied by the mouse event.
179*/
180
181
182/*!
183 \qmlproperty bool QtQuick::MouseEvent::accepted
184
185 Setting \a accepted to true prevents the mouse event from being
186 propagated to items below this item.
187
188 Generally, if the item acts on the mouse event then it should be accepted
189 so that items lower in the stacking order do not also respond to the same event.
190*/
191
192/*!
193 \qmlproperty enumeration QtQuick::MouseEvent::button
194
195 This property holds the button that caused the event. It can be one of:
196 \list
197 \li \l {Qt::LeftButton} {Qt.LeftButton}
198 \li \l {Qt::RightButton} {Qt.RightButton}
199 \li \l {Qt::MiddleButton} {Qt.MiddleButton}
200 \endlist
201*/
202
203/*!
204 \qmlproperty bool QtQuick::MouseEvent::wasHeld
205
206 This property is true if the mouse button has been held pressed longer
207 than the threshold (800ms).
208*/
209
210/*!
211 \qmlproperty int QtQuick::MouseEvent::buttons
212
213 This property holds the mouse buttons pressed when the event was generated.
214 For mouse move events, this is all buttons that are pressed down. For mouse
215 press and double click events this includes the button that caused the event.
216 For mouse release events this excludes the button that caused the event.
217
218 It contains a bitwise combination of:
219 \list
220 \li \l {Qt::LeftButton} {Qt.LeftButton}
221 \li \l {Qt::RightButton} {Qt.RightButton}
222 \li \l {Qt::MiddleButton} {Qt.MiddleButton}
223 \endlist
224*/
225
226/*!
227 \qmlproperty int QtQuick::MouseEvent::modifiers
228
229 This property holds the keyboard modifier flags that existed immediately
230 before the event occurred.
231
232 It contains a bitwise combination of:
233 \list
234 \li \l {Qt::NoModifier} {Qt.NoModifier} - No modifier key is pressed.
235 \li \l {Qt::ShiftModifier} {Qt.ShiftModifier} - A Shift key on the keyboard is pressed.
236 \li \l {Qt::ControlModifier} {Qt.ControlModifier} - A Ctrl key on the keyboard is pressed.
237 \li \l {Qt::AltModifier} {Qt.AltModifier} - An Alt key on the keyboard is pressed.
238 \li \l {Qt::MetaModifier} {Qt.MetaModifier} - A Meta key on the keyboard is pressed.
239 \li \l {Qt::KeypadModifier} {Qt.KeypadModifier} - A keypad button is pressed.
240 \endlist
241
242 For example, to react to a Shift key + Left mouse button click:
243 \qml
244 MouseArea {
245 onClicked: (mouse)=> {
246 if ((mouse.button == Qt.LeftButton) && (mouse.modifiers & Qt.ShiftModifier))
247 doSomething();
248 }
249 }
250 \endqml
251*/
252
253/*!
254 \qmlproperty int QtQuick::MouseEvent::source
255 \since 5.7
256 \deprecated [6.2] Use \l {Qt Quick Input Handlers}{input handlers} with \l {PointerDeviceHandler::acceptedDevices}{acceptedDevices} set.
257
258 This property holds the source of the mouse event.
259
260 The mouse event source can be used to distinguish between genuine and
261 artificial mouse events. When using other pointing devices such as
262 touchscreens and graphics tablets, if the application does not make use of
263 the actual touch or tablet events, mouse events may be synthesized by the
264 operating system or by Qt itself.
265
266 The value can be one of:
267
268 \list
269 \li \l{Qt::MouseEventNotSynthesized} {Qt.MouseEventNotSynthesized}
270 - The most common value. On platforms where such information is
271 available, this value indicates that the event represents a genuine
272 mouse event from the system.
273
274 \li \l{Qt::MouseEventSynthesizedBySystem} {Qt.MouseEventSynthesizedBySystem} - Indicates that the mouse event was
275 synthesized from a touch or tablet event by the platform.
276
277 \li \l{Qt::MouseEventSynthesizedByQt} {Qt.MouseEventSynthesizedByQt}
278 - Indicates that the mouse event was synthesized from an unhandled
279 touch or tablet event by Qt.
280
281 \li \l{Qt::MouseEventSynthesizedByApplication} {Qt.MouseEventSynthesizedByApplication}
282 - Indicates that the mouse event was synthesized by the application.
283 This allows distinguishing application-generated mouse events from
284 the ones that are coming from the system or are synthesized by Qt.
285 \endlist
286
287 For example, to react only to events which come from an actual mouse:
288 \qml
289 MouseArea {
290 onPressed: (mouse)=> {
291 if (mouse.source !== Qt.MouseEventNotSynthesized)
292 mouse.accepted = false
293 }
294
295 onClicked: doSomething()
296 }
297 \endqml
298
299 If the handler for the press event rejects the event, it will be propagated
300 further, and then another Item underneath can handle synthesized events
301 from touchscreens. For example, if a Flickable is used underneath (and the
302 MouseArea is not a child of the Flickable), it can be useful for the
303 MouseArea to handle genuine mouse events in one way, while allowing touch
304 events to fall through to the Flickable underneath, so that the ability to
305 flick on a touchscreen is retained. In that case the ability to drag the
306 Flickable via mouse would be lost, but it does not prevent Flickable from
307 receiving mouse wheel events.
308*/
309
310/*!
311 \qmlproperty int QtQuick::MouseEvent::flags
312 \since 5.11
313
314 This property holds the flags that provide additional information about the
315 mouse event.
316
317 \list
318 \li \l {Qt::MouseEventCreatedDoubleClick} {Qt.MouseEventCreatedDoubleClick}
319 - Indicates that Qt has created a double click event from this event.
320 This flag is set in the event originating from a button press, and not
321 in the resulting double click event.
322 \endlist
323*/
324
325/*!
326 \qmltype WheelEvent
327 \nativetype QQuickWheelEvent
328 \inqmlmodule QtQuick
329 \ingroup qtquick-input-events
330 \brief Provides information about a mouse wheel event.
331
332 The position of the mouse can be found via the \l x and \l y properties.
333
334 \sa WheelHandler, MouseArea
335*/
336
337/*!
338 \internal
339 \class QQuickWheelEvent
340*/
341
342/*!
343 \qmlproperty real QtQuick::WheelEvent::x
344 \qmlproperty real QtQuick::WheelEvent::y
345
346 These properties hold the coordinates of the position supplied by the wheel event.
347
348 \sa QWheelEvent::position()
349*/
350
351/*!
352 \qmlproperty bool QtQuick::WheelEvent::accepted
353
354 Setting \a accepted to \c true prevents the wheel event from being
355 propagated to items below the receiving item or handler.
356
357 Generally, if the item acts on the wheel event, it should be accepted
358 so that items lower in the stacking order do not also respond to the same event.
359
360 \sa QWheelEvent::accepted
361*/
362
363/*!
364 \qmlproperty int QtQuick::WheelEvent::buttons
365
366 This property holds the mouse buttons pressed when the wheel event was generated.
367
368 It contains a bitwise combination of:
369 \list
370 \li \l {Qt::LeftButton} {Qt.LeftButton}
371 \li \l {Qt::RightButton} {Qt.RightButton}
372 \li \l {Qt::MiddleButton} {Qt.MiddleButton}
373 \endlist
374
375 \sa QWheelEvent::buttons()
376*/
377
378/*!
379 \qmlproperty point QtQuick::WheelEvent::angleDelta
380
381 This property holds the relative amount that the wheel was rotated, in
382 eighths of a degree. The \c x and \c y coordinates of this property hold
383 the delta in horizontal and vertical orientations, respectively.
384
385 A positive value indicates that the wheel was rotated up/right;
386 a negative value indicates that the wheel was rotated down/left.
387
388 Most mouse types work in steps of \c 15 degrees, in which case the delta value is a
389 multiple of \c 120; i.e., \c {120 units * 1/8 = 15 degrees}.
390
391 \sa QWheelEvent::angleDelta()
392*/
393
394/*!
395 \qmlproperty point QtQuick::WheelEvent::pixelDelta
396
397 This property holds the delta in screen pixels and is available in platforms that
398 have high-resolution \l {QInputDevice::DeviceType::TouchPad}{trackpads}, such as \macos.
399 The \c x and \c y coordinates of this property hold the delta in horizontal
400 and vertical orientations, respectively. The values can be used directly to
401 scroll content on screen.
402
403 For platforms without \l {QInputDevice::Capability::PixelScroll}{high-resolution trackpad}
404 support, pixelDelta will always be \c {(0,0)}, and \l angleDelta should be used instead.
405
406 \sa QWheelEvent::pixelDelta()
407*/
408
409/*!
410 \qmlproperty int QtQuick::WheelEvent::modifiers
411
412 This property holds the keyboard modifier flags that existed immediately
413 before the event occurred.
414
415 It contains a bitwise combination of:
416 \list
417 \li \l {Qt::NoModifier} {Qt.NoModifier} - No modifier key is pressed.
418 \li \l {Qt::ShiftModifier} {Qt.ShiftModifier} - A Shift key on the keyboard is pressed.
419 \li \l {Qt::ControlModifier} {Qt.ControlModifier} - A Ctrl key on the keyboard is pressed.
420 \li \l {Qt::AltModifier} {Qt.AltModifier} - An Alt key on the keyboard is pressed.
421 \li \l {Qt::MetaModifier} {Qt.MetaModifier} - A Meta key on the keyboard is pressed.
422 \li \l {Qt::KeypadModifier} {Qt.KeypadModifier} - A keypad button is pressed.
423 \endlist
424
425 For example, to react to a Control key pressed during the wheel event:
426 \qml
427 WheelHandler {
428 onWheel: (wheel)=> {
429 if (wheel.modifiers & Qt.ControlModifier) {
430 adjustZoom(wheel.angleDelta.y / 120);
431 }
432 }
433 }
434 \endqml
435
436 \sa QWheelEvent::modifiers()
437*/
438
439/*!
440 \qmlproperty bool QtQuick::WheelEvent::inverted
441
442 Returns whether the delta values delivered with the event are inverted.
443
444 Normally, a vertical wheel will produce a WheelEvent with positive delta
445 values if the top of the wheel is rotating away from the hand operating it.
446 Similarly, a horizontal wheel movement will produce a QWheelEvent with
447 positive delta values if the top of the wheel is moved to the left.
448
449 However, on some platforms this is configurable, so that the same
450 operations described above will produce negative delta values (but with the
451 same magnitude). For instance, in a QML component (such as a tumbler or a
452 slider) where it is appropriate to synchronize the movement or rotation of
453 an item with the direction of the wheel, regardless of the system settings,
454 the wheel event handler can use the inverted property to decide whether to
455 negate the angleDelta or pixelDelta values.
456
457 \note Many platforms provide no such information. On such platforms
458 \c inverted always returns \c false.
459
460 \sa QWheelEvent::inverted()
461*/
462
463QT_END_NAMESPACE
464
465#include "moc_qquickevents_p_p.cpp"