import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow
{
visible: true
id:root
width: 640
height: 480
Rectangle {
id: rectangle1
anchors.fill: parent
focus: true;
Text
{
Keys.enabled: true;
Keys.forwardTo: [text1,like];
id: text1
x: 105
y: 96
width: 107
height: 57
color: "#f62424"
text: qsTr("移动")
antialiasing: false
horizontalAlignment: Text.AlignLeft
font.pixelSize: 50
Keys.onPressed:
{
console.log(event.key);
switch(event.key)
{
case Qt.Key_Left:
x=x-10;
break;
case Qt.Key_Right:
x=x+10;
break;
case Qt.Key_Down:
y=y+10;
break;
case Qt.Key_Up:
y=y-10;
break;
}
event.acceptd=true;
}
}
CheckBox {
id: like
x: 0
y: 466
width: 138
height: 14
text: qsTr("like")
}
}
}
------解决思路----------------------
Keys.onPressed提出来,Text里面接收不到键盘消息
Text
{
Keys.enabled: true;
Keys.forwardTo: [text1,like];
id: text1
x: 105
y: 96
width: 107
height: 57
color: "#f62424"
text: qsTr("移动")
antialiasing: false
horizontalAlignment: Text.AlignLeft
font.pixelSize: 50
}
Keys.onPressed:
{
console.log(event.key);
switch(event.key)
{
case Qt.Key_Left:
text1.x=text1.x-10;
break;
case Qt.Key_Right:
text1.x=text1.x+10;
break;
case Qt.Key_Down:
text1.y=text1.y+10;
break;
case Qt.Key_Up:
text1.y=text1.y-10;
break;
}
event.acceptd=true;
}