KeyEvent QML Type

提供有关关键事件的信息。更多

Import Statement: import QtQuick

属性

方法

  • bool matches(StandardKey matchKey)

详细说明

例如,下面的代码会在按下 Enter 键时更改项的状态属性:

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

属性文档

accepted : bool

accepted 设置为 true 可以防止按键事件传播到项的父项。

一般来说,如果项目对按键事件做出反应,则应接受该事件,以免父项目也对同一事件做出反应。


count : int [read-only]

该属性表示该事件涉及的键的数量。如果KeyEvent::text 不为空,它就是字符串的长度。


isAutoRepeat : bool [read-only]

该属性显示该事件是否来自自动重复键。


key : int [read-only]

该属性包含按下或松开的键的代码。

键盘代码列表请参见Qt.Key 。这些代码与底层窗口系统无关。请注意,该函数不区分大写字母和非大写字母;为此请使用text 属性。

如果值为 0 或Qt.Key_Unknown ,则表示该事件不是已知按键的结果;例如,它可能是编译序列、键盘宏或按键事件压缩的结果。


modifiers : int [read-only]

该属性保存事件发生前的键盘修改器标志。

它包含一个按位组合的数值(与Qt::KeyboardModifier 中的数值相同):

常量说明
Qt.NoModifier未按下修改器键。
Qt.ShiftModifier键盘上的 Shift 键被按下。
Qt.ControlModifier按下键盘上的 Ctrl 键。
Qt.AltModifier键盘上的 Alt 键被按下。
Qt.MetaModifier按下键盘上的 Meta 键。
Qt.KeypadModifier按下键盘按钮。
Qt.GroupSwitchModifier仅限 X11。键盘上的 Mode_switch 键被按下。

例如,对 Shift 键 + Enter 组合键做出反应:

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

nativeScanCode : quint32 [read-only]

该属性包含按下的按键的本地扫描代码。它将原封不动地从QKeyEvent 传递过来。

另请参见 QKeyEvent::nativeScanCode()。


text : string [read-only]

该属性包含按键生成的 Unicode 文本。在按下或释放 Shift、Control、Alt 和 Meta 等修饰符键的情况下,返回的文本可以是空字符串。在这种情况下,key 将包含一个有效值。


方法文档

bool matches(StandardKey matchKey)

如果按键事件符合给定的标准matchKey ,则返回true ;否则返回false

Item {
    focus: true
    Keys.onPressed: (event)=> {
        if (event.matches(StandardKey.Undo))
            myModel.undo();
        else if (event.matches(StandardKey.Redo))
            myModel.redo();
    }
}

另请参见 QKeySequence::StandardKey


© 2025 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.