PDA

View Full Version : Move rectangle with arrow keys



neda
4th May 2016, 08:52
How would I use the arrow keys to move rectangle?


Rectangle {
id:point
anchors.centerIn: parent
width: 20
height: 20

opacity: 0.2

MouseArea {
anchors.fill: parent
drag.target: root
onPositionChanged: {
if(drag.active) {
dragged()
}
}
}
Keys.onPressed: {
if (event.key == Qt.Key_Left) {
console.log("move left");
event.accepted = true;
point.x-=1;
}
}
}

anda_skoa
4th May 2016, 09:41
Looks ok, what doesn't work?
Is the onPressed() called but it doesn't move?

Cheers,
_

neda
8th May 2016, 07:19
Looks ok, what doesn't work?
Is the onPressed() called but it doesn't move?

_

Yes, it does not work. I put my project in this link (http://s000.tinyupload.com/index.php?file_id=00937630145409171677)

anda_skoa
8th May 2016, 08:32
Yes
Actually no, Keys.onPressed() is never called. But fortunately you provided the code so I could see for myself.

1) You need to activate the focus in MouseArea.onPressed
2) You need to activate the focus by calling point.forceActiveFocus()

Cheers,
_

neda
8th May 2016, 12:06
Actually no, Keys.onPressed() is never called. But fortunately you provided the code so I could see for myself.

1) You need to activate the focus in MouseArea.onPressed
2) You need to activate the focus by calling point.forceActiveFocus()

_

Thank you so much dear friend.