Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QKeyEvent Class Reference

The QKeyEvent class describes a key event. More...

#include <qevent.h>

+ Inheritance diagram for QKeyEvent:
+ Collaboration diagram for QKeyEvent:

Public Member Functions

 QKeyEvent (Type type, int key, Qt::KeyboardModifiers modifiers, const QString &text=QString(), bool autorep=false, quint16 count=1)
 Constructs a key event object.
 
 QKeyEvent (Type type, int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, const QString &text=QString(), bool autorep=false, quint16 count=1, const QInputDevice *device=QInputDevice::primaryKeyboard())
 Constructs a key event object.
 
int key () const
 Returns the code of the key that was pressed or released.
 
Qt::KeyboardModifiers modifiers () const
 Returns the keyboard modifier flags that existed immediately after the event occurred.
 
QKeyCombination keyCombination () const
 Returns a QKeyCombination object containing both the key() and the modifiers() carried by this event.
 
QString text () const
 Returns the Unicode text that this key generated.
 
bool isAutoRepeat () const
 Returns true if this event comes from an auto-repeating key; returns false if it comes from an initial key press.
 
int count () const
 Returns the number of keys involved in this event.
 
quint32 nativeScanCode () const
 
quint32 nativeVirtualKey () const
 
quint32 nativeModifiers () const
 
- Public Member Functions inherited from QInputEvent
 QInputEvent (Type type, const QInputDevice *m_dev, Qt::KeyboardModifiers modifiers=Qt::NoModifier)
 
const QInputDevicedevice () const
 
QInputDevice::DeviceType deviceType () const
 Returns the type of device that generated the event.
 
Qt::KeyboardModifiers modifiers () const
 Returns the keyboard modifier flags that existed immediately before the event occurred.
 
void setModifiers (Qt::KeyboardModifiers modifiers)
 
quint64 timestamp () const
 Returns the window system's timestamp for this event.
 
virtual void setTimestamp (quint64 timestamp)
 
- Public Member Functions inherited from QEvent
 QEvent (Type type)
 Constructs an event object of type type.
 
virtual ~QEvent ()
 Destroys the event.
 
Type type () const
 Returns the event type.
 
bool spontaneous () const
 Returns true if the event originated outside the application (a system event); otherwise returns false.
 
virtual void setAccepted (bool accepted)
 
bool isAccepted () const
 
void accept ()
 Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
 
void ignore ()
 Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
 
bool isInputEvent () const noexcept
 
bool isPointerEvent () const noexcept
 
bool isSinglePointEvent () const noexcept
 
virtual QEventclone () const
 Creates and returns an identical copy of this event.
 

Protected Attributes

QString m_text
 
int m_key
 
quint32 m_scanCode
 
quint32 m_virtualKey
 
quint32 m_nativeModifiers
 
quint16 m_count: 15
 
quint16 m_autoRepeat: 1
 
- Protected Attributes inherited from QInputEvent
const QInputDevicem_dev = nullptr
 
quint64 m_timeStamp = 0
 
Qt::KeyboardModifiers m_modState = Qt::NoModifier
 
quint32 m_reserved = 0
 
- Protected Attributes inherited from QEvent
quint16 t
 

Additional Inherited Members

- Public Types inherited from QEvent
enum  Type {
  None = 0 , Timer = 1 , MouseButtonPress = 2 , MouseButtonRelease = 3 ,
  MouseButtonDblClick = 4 , MouseMove = 5 , KeyPress = 6 , KeyRelease = 7 ,
  FocusIn = 8 , FocusOut = 9 , FocusAboutToChange = 23 , Enter = 10 ,
  Leave = 11 , Paint = 12 , Move = 13 , Resize = 14 ,
  Create = 15 , Destroy = 16 , Show = 17 , Hide = 18 ,
  Close = 19 , Quit = 20 , ParentChange = 21 , ParentAboutToChange = 131 ,
  ThreadChange = 22 , WindowActivate = 24 , WindowDeactivate = 25 , ShowToParent = 26 ,
  HideToParent = 27 , Wheel = 31 , WindowTitleChange = 33 , WindowIconChange = 34 ,
  ApplicationWindowIconChange = 35 , ApplicationFontChange = 36 , ApplicationLayoutDirectionChange = 37 , ApplicationPaletteChange = 38 ,
  PaletteChange = 39 , Clipboard = 40 , Speech = 42 , MetaCall = 43 ,
  SockAct = 50 , WinEventAct = 132 , DeferredDelete = 52 , DragEnter = 60 ,
  DragMove = 61 , DragLeave = 62 , Drop = 63 , DragResponse = 64 ,
  ChildAdded = 68 , ChildPolished = 69 , ChildRemoved = 71 , ShowWindowRequest = 73 ,
  PolishRequest = 74 , Polish = 75 , LayoutRequest = 76 , UpdateRequest = 77 ,
  UpdateLater = 78 , EmbeddingControl = 79 , ActivateControl = 80 , DeactivateControl = 81 ,
  ContextMenu = 82 , InputMethod = 83 , TabletMove = 87 , LocaleChange = 88 ,
  LanguageChange = 89 , LayoutDirectionChange = 90 , Style = 91 , TabletPress = 92 ,
  TabletRelease = 93 , OkRequest = 94 , HelpRequest = 95 , IconDrag = 96 ,
  FontChange = 97 , EnabledChange = 98 , ActivationChange = 99 , StyleChange = 100 ,
  IconTextChange = 101 , ModifiedChange = 102 , MouseTrackingChange = 109 , WindowBlocked = 103 ,
  WindowUnblocked = 104 , WindowStateChange = 105 , ReadOnlyChange = 106 , ToolTip = 110 ,
  WhatsThis = 111 , StatusTip = 112 , ActionChanged = 113 , ActionAdded = 114 ,
  ActionRemoved = 115 , FileOpen = 116 , Shortcut = 117 , ShortcutOverride = 51 ,
  WhatsThisClicked = 118 , ToolBarChange = 120 , ApplicationActivate = 121 , ApplicationActivated = ApplicationActivate ,
  ApplicationDeactivate = 122 , ApplicationDeactivated = ApplicationDeactivate , QueryWhatsThis = 123 , EnterWhatsThisMode = 124 ,
  LeaveWhatsThisMode = 125 , ZOrderChange = 126 , HoverEnter = 127 , HoverLeave = 128 ,
  HoverMove = 129 , AcceptDropsChange = 152 , ZeroTimerEvent = 154 , GraphicsSceneMouseMove = 155 ,
  GraphicsSceneMousePress = 156 , GraphicsSceneMouseRelease = 157 , GraphicsSceneMouseDoubleClick = 158 , GraphicsSceneContextMenu = 159 ,
  GraphicsSceneHoverEnter = 160 , GraphicsSceneHoverMove = 161 , GraphicsSceneHoverLeave = 162 , GraphicsSceneHelp = 163 ,
  GraphicsSceneDragEnter = 164 , GraphicsSceneDragMove = 165 , GraphicsSceneDragLeave = 166 , GraphicsSceneDrop = 167 ,
  GraphicsSceneWheel = 168 , GraphicsSceneLeave = 220 , KeyboardLayoutChange = 169 , DynamicPropertyChange = 170 ,
  TabletEnterProximity = 171 , TabletLeaveProximity = 172 , NonClientAreaMouseMove = 173 , NonClientAreaMouseButtonPress = 174 ,
  NonClientAreaMouseButtonRelease = 175 , NonClientAreaMouseButtonDblClick = 176 , MacSizeChange = 177 , ContentsRectChange = 178 ,
  MacGLWindowChange = 179 , FutureCallOut = 180 , GraphicsSceneResize = 181 , GraphicsSceneMove = 182 ,
  CursorChange = 183 , ToolTipChange = 184 , NetworkReplyUpdated = 185 , GrabMouse = 186 ,
  UngrabMouse = 187 , GrabKeyboard = 188 , UngrabKeyboard = 189 , StateMachineSignal = 192 ,
  StateMachineWrapped = 193 , TouchBegin = 194 , TouchUpdate = 195 , TouchEnd = 196 ,
  NativeGesture = 197 , RequestSoftwareInputPanel = 199 , CloseSoftwareInputPanel = 200 , WinIdChange = 203 ,
  Gesture = 198 , GestureOverride = 202 , ScrollPrepare = 204 , Scroll = 205 ,
  Expose = 206 , InputMethodQuery = 207 , OrientationChange = 208 , TouchCancel = 209 ,
  ThemeChange = 210 , SockClose = 211 , PlatformPanel = 212 , StyleAnimationUpdate = 213 ,
  ApplicationStateChange = 214 , WindowChangeInternal = 215 , ScreenChangeInternal = 216 , PlatformSurface = 217 ,
  Pointer = 218 , TabletTrackingChange = 219 , WindowAboutToChangeInternal = 221 , DevicePixelRatioChange = 222 ,
  ChildWindowAdded = 223 , ChildWindowRemoved = 224 , ParentWindowAboutToChange = 225 , ParentWindowChange = 226 ,
  User = 1000 , MaxUser = 65535
}
 This enum type defines the valid event types in Qt. More...
 
- Static Public Member Functions inherited from QEvent
static int registerEventType (int hint=-1) noexcept
 
- Protected Member Functions inherited from QInputEvent
 QInputEvent (Type type, PointerEventTag, const QInputDevice *dev, Qt::KeyboardModifiers modifiers=Qt::NoModifier)
 
 QInputEvent (Type type, SinglePointEventTag, const QInputDevice *dev, Qt::KeyboardModifiers modifiers=Qt::NoModifier)
 
- Protected Member Functions inherited from QEvent
 QEvent (Type type, InputEventTag)
 
 QEvent (Type type, PointerEventTag)
 
 QEvent (Type type, SinglePointEventTag)
 

Detailed Description

The QKeyEvent class describes a key event.

\inmodule QtGui

Key events are sent to the widget with keyboard input focus when keys are pressed or released.

A key event contains a special accept flag that indicates whether the receiver will handle the key event. This flag is set by default for QEvent::KeyPress and QEvent::KeyRelease, so there is no need to call accept() when acting on a key event. For QEvent::ShortcutOverride the receiver needs to explicitly accept the event to trigger the override. Calling ignore() on a key event will propagate it to the parent widget. The event is propagated up the parent widget chain until a widget accepts it or an event filter consumes it.

The QWidget::setEnabled() function can be used to enable or disable mouse and keyboard events for a widget.

The event handlers QWidget::keyPressEvent(), QWidget::keyReleaseEvent(), QGraphicsItem::keyPressEvent() and QGraphicsItem::keyReleaseEvent() receive key events.

See also
QFocusEvent, QWidget::grabKeyboard()

Definition at line 423 of file qevent.h.

Constructor & Destructor Documentation

◆ QKeyEvent() [1/2]

QKeyEvent::QKeyEvent ( Type type,
int key,
Qt::KeyboardModifiers modifiers,
const QString & text = QString(),
bool autorep = false,
quint16 count = 1 )

Constructs a key event object.

The type parameter must be QEvent::KeyPress, QEvent::KeyRelease, or QEvent::ShortcutOverride.

Int key is the code for the Qt::Key that the event loop should listen for. If key is 0, the event is not a result of a known key; for example, it may be the result of a compose sequence or keyboard macro. The modifiers holds the keyboard modifiers, and the given text is the Unicode text that the key generated. If autorep is true, isAutoRepeat() will be true. count is the number of keys involved in the event.

Definition at line 1349 of file qevent.cpp.

References QEvent::ignore(), and QEvent::ShortcutOverride.

+ Here is the call graph for this function:

◆ QKeyEvent() [2/2]

QKeyEvent::QKeyEvent ( Type type,
int key,
Qt::KeyboardModifiers modifiers,
quint32 nativeScanCode,
quint32 nativeVirtualKey,
quint32 nativeModifiers,
const QString & text = QString(),
bool autorep = false,
quint16 count = 1,
const QInputDevice * device = QInputDevice::primaryKeyboard() )

Constructs a key event object.

The type parameter must be QEvent::KeyPress, QEvent::KeyRelease, or QEvent::ShortcutOverride.

Int key is the code for the Qt::Key that the event loop should listen for. If key is 0, the event is not a result of a known key; for example, it may be the result of a compose sequence or keyboard macro. The modifiers holds the keyboard modifiers, and the given text is the Unicode text that the key generated. If autorep is true, isAutoRepeat() will be true. count is the number of keys involved in the event.

In addition to the normal key event data, also contains nativeScanCode, nativeVirtualKey and nativeModifiers. This extra data is used by the shortcut system, to determine which shortcuts to trigger.

Definition at line 1377 of file qevent.cpp.

References QEvent::ignore(), and QEvent::ShortcutOverride.

+ Here is the call graph for this function:

Member Function Documentation

◆ count()

int QKeyEvent::count ( ) const
inline

Returns the number of keys involved in this event.

If text() is not empty, this is simply the length of the string.

See also
Qt::WA_KeyCompression

Definition at line 445 of file qevent.h.

Referenced by QQuickDeliveryAgentPrivate::deliverKeyEvent(), and QApplication::notify().

+ Here is the caller graph for this function:

◆ isAutoRepeat()

bool QKeyEvent::isAutoRepeat ( ) const
inline

Returns true if this event comes from an auto-repeating key; returns false if it comes from an initial key press.

Note that if the event is a multiple-key compressed event that is partly due to auto-repeat, this function could return either true or false indeterminately.

Definition at line 444 of file qevent.h.

Referenced by QQuickDeliveryAgentPrivate::deliverKeyEvent(), QGroupBox::event(), QIBusPlatformInputContext::filterEvent(), QTabWidget::keyPressEvent(), QAbstractButton::keyPressEvent(), QPlainTextEdit::keyPressEvent(), QTextEdit::keyPressEvent(), QAbstractButton::keyReleaseEvent(), QPlainTextEdit::keyReleaseEvent(), QTextEdit::keyReleaseEvent(), and QApplication::notify().

+ Here is the caller graph for this function:

◆ key()

int QKeyEvent::key ( ) const
inline

Returns the code of the key that was pressed or released.

See \l Qt::Key for the list of keyboard codes. These codes are independent of the underlying window system. Note that this function does not distinguish between capital and non-capital letters, use the text() function (returning the Unicode text the key generated) for this purpose.

A value of either 0 or Qt::Key_unknown means that the event is not the result of a known key; for example, it may be the result of a compose sequence, a keyboard macro, or due to key event compression.

See also
Qt::WA_KeyCompression

Definition at line 434 of file qevent.h.

Referenced by QWidgetTextControlPrivate::cursorMoveKeyEvent(), QQuickTextControlPrivate::cursorMoveKeyEvent(), QQuickItemPrivate::deliverKeyEvent(), QQuickDeliveryAgentPrivate::deliverKeyEvent(), QQuickTableViewPrivate::editFromKeyEvent(), QItemDelegate::editorEvent(), QStyledItemDelegate::editorEvent(), QComboMenuDelegate::editorEvent(), QAbstractItemDelegatePrivate::editorEventFilter(), QGraphicsProxyWidget::event(), QGraphicsScene::event(), QGraphicsView::event(), QWidget::event(), QGroupBox::event(), QBasicDrag::eventFilter(), QComboBoxPrivateContainer::eventFilter(), MainWindow::eventFilter(), KeyPressEater::eventFilter(), QQuickTableView::eventFilter(), FilterObject::eventFilter(), QFontDialog::eventFilter(), QMdiArea::eventFilter(), QQuickMenuBar::eventFilter(), QIBusPlatformInputContext::filterEvent(), QColorDialogPrivate::handleColorPickingKeyPress(), QtPrivate::QCalendarDateValidator::handleKeyEvent(), QtPrivate::QWellArray::keyPressEvent(), QDialog::keyPressEvent(), QKeySequenceEdit::keyPressEvent(), QMenu::keyPressEvent(), QMenuBar::keyPressEvent(), QPushButton::keyPressEvent(), QTabWidget::keyPressEvent(), QQuickWidget::keyPressEvent(), QWidgetResizeHandler::keyPressEvent(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), QFileDialogLineEdit::keyPressEvent(), QAbstractButton::keyPressEvent(), QComboBox::keyPressEvent(), QPlainTextEdit::keyPressEvent(), QTextEdit::keyPressEvent(), QMessageBox::keyPressEvent(), QMdiSubWindow::keyPressEvent(), QKeySequenceEdit::keyReleaseEvent(), QQuickWidget::keyReleaseEvent(), QAbstractButton::keyReleaseEvent(), QPlainTextEdit::keyReleaseEvent(), QTextEdit::keyReleaseEvent(), QApplication::notify(), QKeyMapper::possibleKeys(), QGraphicsItem::sceneEvent(), QQuickTableViewPrivate::setCurrentIndexFromKeyEvent(), and QShortcutMap::tryShortcut().

◆ keyCombination()

QKeyCombination QKeyEvent::keyCombination ( ) const
inline

Returns a QKeyCombination object containing both the key() and the modifiers() carried by this event.

Since
6.0

Definition at line 439 of file qevent.h.

Referenced by QKeySequenceEdit::keyPressEvent(), and QKeyMapper::possibleKeys().

+ Here is the caller graph for this function:

◆ modifiers()

Qt::KeyboardModifiers QKeyEvent::modifiers ( ) const

Returns the keyboard modifier flags that existed immediately after the event occurred.

Warning
This function cannot always be trusted. The user can confuse it by pressing both \uicontrol{Shift} keys simultaneously and releasing one of them, for example.
See also
QGuiApplication::keyboardModifiers()

Definition at line 1468 of file qevent.cpp.

References Qt::AltModifier, Qt::ControlModifier, Qt::GroupSwitchModifier, Qt::Key_Alt, Qt::Key_AltGr, Qt::Key_Control, Qt::Key_Meta, Qt::Key_Shift, Qt::MetaModifier, QInputEvent::modifiers(), and Qt::ShiftModifier.

Referenced by QQuickItemPrivate::deliverKeyEvent(), QQuickDeliveryAgentPrivate::deliverKeyEvent(), QGraphicsProxyWidget::event(), QGraphicsScene::event(), QWidget::event(), QComboBoxPrivateContainer::eventFilter(), QMdiArea::eventFilter(), QQuickMenuBar::eventFilter(), QIBusPlatformInputContext::filterEvent(), QDialog::keyPressEvent(), QKeySequenceEdit::keyPressEvent(), QMenu::keyPressEvent(), QMenuBar::keyPressEvent(), QTabWidget::keyPressEvent(), QQuickWidget::keyPressEvent(), QWidgetResizeHandler::keyPressEvent(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), QComboBox::keyPressEvent(), QPlainTextEdit::keyPressEvent(), QTextEdit::keyPressEvent(), QMessageBox::keyPressEvent(), QMdiSubWindow::keyPressEvent(), QQuickWidget::keyReleaseEvent(), QApplication::notify(), QWindowsKeyMapper::possibleKeyCombinations(), QKeyMapper::possibleKeys(), QGraphicsItem::sceneEvent(), and QQuickTableViewPrivate::setCurrentIndexFromKeyEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nativeModifiers()

quint32 QKeyEvent::nativeModifiers ( ) const
inline
Since
4.2

Returns the native modifiers of a key event. If the key event does not contain this data 0 is returned.

Note
The native modifiers may be 0, even if the key event contains extended information.

Definition at line 449 of file qevent.h.

Referenced by QIBusPlatformInputContext::filterEvent(), and QWindowsKeyMapper::possibleKeyCombinations().

+ Here is the caller graph for this function:

◆ nativeScanCode()

quint32 QKeyEvent::nativeScanCode ( ) const
inline
Since
4.2

Returns the native scan code of the key event. If the key event does not contain this data 0 is returned.

Note
The native scan code may be 0, even if the key event contains extended information.

Definition at line 447 of file qevent.h.

Referenced by QIBusPlatformInputContext::filterEvent().

+ Here is the caller graph for this function:

◆ nativeVirtualKey()

quint32 QKeyEvent::nativeVirtualKey ( ) const
inline
Since
4.2

Returns the native virtual key, or key sym of the key event. If the key event does not contain this data 0 is returned.

Note
The native virtual key may be 0, even if the key event contains extended information.

Definition at line 448 of file qevent.h.

Referenced by QIBusPlatformInputContext::filterEvent(), and QWindowsKeyMapper::possibleKeyCombinations().

+ Here is the caller graph for this function:

◆ text()

QString QKeyEvent::text ( ) const
inline

Returns the Unicode text that this key generated.

The text is not limited to the printable range of Unicode code points, and may include control characters or characters from other Unicode categories, including QChar::Other_PrivateUse.

The text may also be empty, for example when modifier keys such as Shift, Control, Alt, and Meta are pressed (depending on the platform). The key() function will always return a valid value.

See also
Qt::WA_KeyCompression

Definition at line 443 of file qevent.h.

Referenced by QQuickDeliveryAgentPrivate::deliverKeyEvent(), QIBusPlatformInputContext::filterEvent(), QKeySequenceEdit::keyPressEvent(), QMenu::keyPressEvent(), QMenuBar::keyPressEvent(), QWidgetTextControlPrivate::keyPressEvent(), QQuickTextControlPrivate::keyPressEvent(), QComboBox::keyPressEvent(), QPlainTextEdit::keyPressEvent(), QTextEdit::keyPressEvent(), QApplication::notify(), and QKeyMapper::possibleKeys().

+ Here is the caller graph for this function:

Member Data Documentation

◆ m_autoRepeat

quint16 QKeyEvent::m_autoRepeat
protected

Definition at line 465 of file qevent.h.

◆ m_count

quint16 QKeyEvent::m_count
protected

Definition at line 464 of file qevent.h.

◆ m_key

int QKeyEvent::m_key
protected

Definition at line 460 of file qevent.h.

◆ m_nativeModifiers

quint32 QKeyEvent::m_nativeModifiers
protected

Definition at line 463 of file qevent.h.

◆ m_scanCode

quint32 QKeyEvent::m_scanCode
protected

Definition at line 461 of file qevent.h.

◆ m_text

QString QKeyEvent::m_text
protected

Definition at line 459 of file qevent.h.

◆ m_virtualKey

quint32 QKeyEvent::m_virtualKey
protected

Definition at line 462 of file qevent.h.


The documentation for this class was generated from the following files: