QtBase  v6.3.1
Public Member Functions | Protected Attributes | List of all members
QKeyEvent Class Reference

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

#include <qevent.h>

Inheritance diagram for QKeyEvent:
Inheritance graph
[legend]
Collaboration diagram for QKeyEvent:
Collaboration graph
[legend]

Public Member Functions

 QKeyEvent (Type type, int key, Qt::KeyboardModifiers modifiers, const QString &text=QString(), bool autorep=false, quint16 count=1)
 
 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())
 
 ~QKeyEvent ()
 
QKeyEventclone () const override
 
int key () const
 
Qt::KeyboardModifiers modifiers () const
 
QKeyCombination keyCombination () const
 
QString text () const
 
bool isAutoRepeat () const
 
int count () const
 
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)
 
 ~QInputEvent ()
 
const QInputDevicedevice () const
 
QInputDevice::DeviceType deviceType () const
 
Qt::KeyboardModifiers modifiers () const
 
void setModifiers (Qt::KeyboardModifiers modifiers)
 
quint64 timestamp () const
 
virtual void setTimestamp (quint64 timestamp)
 
- Public Member Functions inherited from QEvent
 QEvent (Type type)
 
virtual ~QEvent ()
 
Type type () const
 
bool spontaneous () const
 
virtual void setAccepted (bool accepted)
 
bool isAccepted () const
 
void accept ()
 
void ignore ()
 
bool isInputEvent () const noexcept
 
bool isPointerEvent () const noexcept
 
bool isSinglePointEvent () const noexcept
 

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 , User = 1000 , MaxUser = 65535
}
 
- 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 470 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 1379 of file qevent.cpp.

◆ 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 1407 of file qevent.cpp.

◆ ~QKeyEvent()

QKeyEvent::~QKeyEvent ( )

Definition at line 1422 of file qevent.cpp.

Member Function Documentation

◆ clone()

QKeyEvent* QKeyEvent::clone ( ) const
inlineoverridevirtual

Creates and returns an identical copy of this event.

Since
6.0

Reimplemented from QInputEvent.

Definition at line 482 of file qevent.h.

◆ 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 495 of file qevent.h.

◆ 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 494 of file qevent.h.

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 484 of file qevent.h.

Here is the caller graph for this function:

◆ 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 489 of file qevent.h.

◆ 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 1502 of file qevent.cpp.

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 499 of file qevent.h.

◆ 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 497 of file qevent.h.

◆ 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 498 of file qevent.h.

◆ text()

QString QKeyEvent::text ( ) const
inline

Returns the Unicode text that this key generated.

Return values when modifier keys such as Shift, Control, Alt, and Meta are pressed differ among platforms and could return an empty string.

Note
\l key() will always return a valid value, independent of modifier keys.
See also
Qt::WA_KeyCompression

Definition at line 493 of file qevent.h.

Here is the caller graph for this function:

Member Data Documentation

◆ m_autoRepeat

quint16 QKeyEvent::m_autoRepeat
protected

Definition at line 515 of file qevent.h.

◆ m_count

quint16 QKeyEvent::m_count
protected

Definition at line 514 of file qevent.h.

◆ m_key

int QKeyEvent::m_key
protected

Definition at line 510 of file qevent.h.

◆ m_nativeModifiers

quint32 QKeyEvent::m_nativeModifiers
protected

Definition at line 513 of file qevent.h.

◆ m_scanCode

quint32 QKeyEvent::m_scanCode
protected

Definition at line 511 of file qevent.h.

◆ m_text

QString QKeyEvent::m_text
protected

Definition at line 509 of file qevent.h.

◆ m_virtualKey

quint32 QKeyEvent::m_virtualKey
protected

Definition at line 512 of file qevent.h.


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