-
Keys对象
Keys对象是Qt Quick提供专门为Item处理按键事件的对象 -
最常用的信号就是onPressed和onReleased信号。这两个信号是针对大部分的按键的。需要注意的是如果有预先定义信号并且处理了的,是无法再触发onPressed信号。比如说数字按键1,如果先响应onDigit1Pressed了,就不会再去触发onPressed信号
-
onAsteriskPressed(KeyEvent event) [对应按键“*”]
-
onBackPressed(KeyEvent event) [对应按键“*”]
-
onBacktabPressed(KeyEvent event) [对应按键“Shift+Tab”]
-
onCallPressed(KeyEvent event)
-
onCancelPressed(KeyEvent event)
-
onContext1Pressed(KeyEvent event)
-
onContext2Pressed(KeyEvent event)
-
onContext3Pressed(KeyEvent event)
-
onContext4Pressed(KeyEvent event)
-
onDeletePressed(KeyEvent event) [对应按键“del”]
-
onDigit0~9Pressed(KeyEvent event) [对应按键“0~9”]
-
onDownPressed(KeyEvent event) [对应按键“下”]
-
onEnterPressed(KeyEvent event) [对应按键“Enter”]
-
onEscapePressed(KeyEvent event) [对应按键“Esc”]
-
onFlipPressed(KeyEvent event)
-
onHangupPressed(KeyEvent event)
-
onLeftPressed(KeyEvent event) [对应按键“左”]
-
onMenuPressed(KeyEvent event) [对应按键“菜单”]
-
onNoPressed(KeyEvent event)
-
onReturnPressed(KeyEvent event) [对应按键“Enter”]
-
onRightPressed(KeyEvent event) [对应按键“右”]
-
onSelectPressed(KeyEvent event)
-
onSpacePressed(KeyEvent event) [对应按键“空格”]
-
onTabPressed(KeyEvent event) [对应按键“Tab”]
-
onUpPressed(KeyEvent event) [对应按键“上”]
-
onVolumeDownPressed(KeyEvent event)
-
onVolumeUpPressed(KeyEvent event)
-
onYesPressed(KeyEvent event)
-
Keys三个属性
-
enabled:控制是否处理按键
-
forward:列表属性,它表示传递按键事件给列表内的对象,如果某个对象accept了某个按键,那位列其后的对象就不会收到该按键事件。
-
priority:设置Keys附件属性的优先级。在Item之前或是之后或者之后处理按键
-
例子
-
当按下Esc键时,程序退出
方向移动的演示,所以选择上下左右四个按键。每次按键时,相应的x、y坐标都会变化,[Move me !]这段文字会相应移动
当你按下空格键时,Checkbox会自动选中或取消选中
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
visible: true
width: 240
height: 240
title: qsTr("event")
Rectangle {
anchors.fill: parent
color: "gray";
focus: true
Keys.enabled: true
Keys.onEscapePressed: {
Qt.quit()
}
Keys.forwardTo: [text, checkbox]
}
Text {
id: text
x: 20
y: 20
width: 200
height: 30
text: "Move me !"
color: "blue"
font {
bold: true
pixelSize: 24
}
Keys.onPressed: {
switch(event.key){
case Qt.Key_Left:
x -= 10
break
case Qt.Key_Right:
x += 10
break
case Qt.Key_Down:
y += 10
break
case Qt.Key_Up:
y -= 10
break
default:
return
}
event.accepted = true
}
}
CheckBox {
id: checkbox
text: "Select me!"
anchors.left: parent.left
anchors.leftMargin: 10
anchors.bottom: parent.bottom
anchors.bottomMargin: 10
}
}