QML开发——键盘响应事件

空扰寡人 提交于 2020-03-01 22:38:57

效果动图

关键注意转移属性值元素的写法

KeyNavigation.tab: play //按下tab键后转到属性值为play的元素上来

源码

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.VirtualKeyboard 2.4

Window {
    id: window
    visible: true
    width: 640
    height: 400
    title: qsTr("Keyboard")
    Rectangle{
        MouseArea{
            id: mouseArea
            anchors.fill: parent
        }
        Row{    //水平布局
            x: 50
            y: 50
            Rectangle{
                id: music
                width: 100
                height: 100
                radius: 6
                color: focus ? "red" : "lightgray"  //没有被选中时显示为灰色;没有被选中时显示灰色
                scale: focus ? 1 : 0.8  //被选中时图标变大;没有被选中时图标正常大小
                focus: true //初始时为选中状态
                KeyNavigation.tab: play //按下tab键后转到属性值为play的元素上来
                Keys.onUpPressed: music.y -= 10  //上移
                Keys.onDownPressed: music.y += 10   //下移
                Keys.onLeftPressed: music.x -= 10   //左移
                Keys.onRightPressed: music.x += 10  //右移
                Text {
                    anchors.centerIn: parent
                    color: parent.focus ? "black" : "gray"  //被选中时文字显示为黑色;没有被选中时显示为灰色
                    font.pointSize: 20
                    text: "音乐"
                }
            }
            Rectangle{
                id: play
                width: 100
                height: 100
                radius: 6
                color: focus ? "green" : "lightgray"  //没有被选中时显示为灰色;没有被选中时显示灰色
                scale: focus ? 1 : 0.8  //被选中时图标变大;没有被选中时图标正常大小
                focus: true //初始时为选中状态
                KeyNavigation.tab: movie //按下tab键后转到属性值为movie的元素上来
                Keys.onUpPressed: play.y -= 10  //上移
                Keys.onDownPressed: play.y += 10   //下移
                Keys.onLeftPressed: play.x -= 10   //左移
                Keys.onRightPressed: play.x += 10  //右移
                Text {
                    anchors.centerIn: parent
                    color: parent.focus ? "black" : "gray"  //被选中时文字显示为黑色;没有被选中时显示为灰色
                    font.pointSize: 20
                    text: "游戏"
                }
            }
            Rectangle{
                id: movie
                width: 100
                height: 100
                radius: 6
                color: focus ? "blue" : "lightgray"  //没有被选中时显示为灰色;没有被选中时显示灰色
                scale: focus ? 1 : 0.8  //被选中时图标变大;没有被选中时图标正常大小
                focus: true //初始时为选中状态
                KeyNavigation.tab: music //按下tab键后转到属性值为movie的元素上来
                Keys.onUpPressed: movie.y -= 10  //上移
                Keys.onDownPressed: movie.y += 10   //下移
                Keys.onLeftPressed: movie.x -= 10   //左移
                Keys.onRightPressed: movie.x += 10  //右移
                Text {
                    anchors.centerIn: parent
                    color: parent.focus ? "black" : "gray"  //被选中时文字显示为黑色;没有被选中时显示为灰色
                    font.pointSize: 20
                    text: "影视"
                }
            }
        }
    }

    InputPanel {
        id: inputPanel
        z: 99
        x: 0
        y: window.height
        width: window.width

        states: State {
            name: "visible"
            when: inputPanel.active
            PropertyChanges {
                target: inputPanel
                y: window.height - inputPanel.height
            }
        }
        transitions: Transition {
            from: ""
            to: "visible"
            reversible: true
            ParallelAnimation {
                NumberAnimation {
                    properties: "y"
                    duration: 250
                    easing.type: Easing.InOutQuad
                }
            }
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!