QKeyEvent Class

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

Header: #include <QKeyEvent>
qmake: QT += gui
Inherits: QInputEvent

Public Functions

QKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, const QString &text = QString(), bool autorep = false, ushort count = 1)
QKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, const QString &text = QString(), bool autorep = false, ushort count = 1)
int count() const
bool hasExtendedInfo() const
bool isAutoRepeat() const
int key() const
bool matches(QKeySequence::StandardKey key) const
Qt::KeyboardModifiers modifiers() const
quint32 nativeModifiers() const
quint32 nativeScanCode() const
quint32 nativeVirtualKey() const
QString text() const
  • 4 public functions inherited from QInputEvent
  • 6 public functions inherited from QEvent

Static Public Members

QKeyEvent *createExtendedKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, const QString &text = QString(), bool autorep = false, ushort count = 1)
  • 1 static public member inherited from QEvent

Protected Variables

ushort autor
ushort c
int k
quint32 nModifiers
quint32 nScanCode
quint32 nVirtualKey
QString txt

Additional Inherited Members

  • 1 property inherited from QEvent

Detailed Description

The QKeyEvent class describes a key event.

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 and QWidget::grabKeyboard().

Member Function Documentation

QKeyEvent::QKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, const QString &text = QString(), bool autorep = false, ushort 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.

QKeyEvent::QKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, const QString &text = QString(), bool autorep = false, ushort 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.

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.

int QKeyEvent::count() const

[static] QKeyEvent *QKeyEvent::createExtendedKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, const QString &text = QString(), bool autorep = false, ushort count = 1)

bool QKeyEvent::hasExtendedInfo() const

bool QKeyEvent::isAutoRepeat() const

int QKeyEvent::key() const

bool QKeyEvent::matches(QKeySequence::StandardKey key) const

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 Shift keys simultaneously and releasing one of them, for example.

See also QGuiApplication::keyboardModifiers().

quint32 QKeyEvent::nativeModifiers() const

quint32 QKeyEvent::nativeScanCode() const

quint32 QKeyEvent::nativeVirtualKey() const

QString QKeyEvent::text() const