KeyEvent QML Type (Uncreatable)
提供有关某个关键事件的信息。更多内容...
| Import Statement: | import QtQuick |
注意:此类型不可创建,无法在 QML 中实例化。
属性
- accepted : bool
- count : int
- isAutoRepeat : bool
- key : int
- modifiers : int
- nativeScanCode : quint32
- text : string
方法
- bool matches(StandardKey matchKey)
详细说明
例如,以下代码会在按下 Enter 键时更改 Item 的 state 属性:
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 。
© 2026 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.