KeyEvent QML Type

Provides information about a key event. More...

Import Statement: import QtQuick



  • bool matches(StandardKey matchKey)

Detailed Description

For example, the following changes the Item's state property when the Enter key is pressed:

Item {
    focus: true
    Keys.onPressed: (event)=> { if (event.key == Qt.Key_Enter) state = 'ShowDetails'; }

Property Documentation

accepted : bool

Setting accepted to true prevents the key event from being propagated to the item's parent.

Generally, if the item acts on the key event then it should be accepted so that ancestor items do not also respond to the same event.

count : int

This property holds the number of keys involved in this event. If KeyEvent::text is not empty, this is simply the length of the string.

isAutoRepeat : bool

This property holds whether this event comes from an auto-repeating key.

key : int

This property holds the code of the key that was pressed or released.

See 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 property 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.

modifiers : int

This property holds the keyboard modifier flags that existed immediately before the event occurred.

It contains a bitwise combination of numeric values (the same as in Qt::KeyboardModifier):

Qt.NoModifierNo modifier key is pressed.
Qt.ShiftModifier}A Shift key on the keyboard is pressed.
Qt.ControlModifierA Ctrl key on the keyboard is pressed.
Qt.AltModifierAn Alt key on the keyboard is pressed.
Qt.MetaModifierA Meta key on the keyboard is pressed.
Qt.KeypadModifierA keypad button is pressed.
Qt.GroupSwitchModifierX11 only. A Mode_switch key on the keyboard is pressed.

For example, to react to a Shift key + Enter key combination:

Item {
    focus: true
    Keys.onPressed: (event)=> {
        if ((event.key == Qt.Key_Enter) && (event.modifiers & Qt.ShiftModifier))

nativeScanCode : quint32

This property contains the native scan code of the key that was pressed. It is passed through from QKeyEvent unchanged.

See also QKeyEvent::nativeScanCode().

text : string

This property holds the Unicode text that the key generated. The text returned can be an empty string in cases where modifier keys, such as Shift, Control, Alt, and Meta, are being pressed or released. In such cases key will contain a valid value

Method Documentation

[since 5.2] bool matches(StandardKey matchKey)

Returns true if the key event matches the given standard matchKey; otherwise returns false.

Item {
    focus: true
    Keys.onPressed: (event)=> {
        if (event.matches(StandardKey.Undo))
        else if (event.matches(StandardKey.Redo))

This method was introduced in Qt 5.2.

See also QKeySequence::StandardKey.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.