如何更改QT虚拟键盘转换逻辑

时间:2019-03-08 15:17:28

标签: qt qml virtual-keyboard

我已经使用QT 5.11为QT虚拟键盘定义了自定义样式和布局。

当您双击Shift键时,QT虚拟键盘Shift键的默认实现将启用Caps Lock。

如何更改实现以基于周期的过程工作?

例如:

  • 拳头点击:启用首字母大写输入。

  • 第二次点击:启用永久的大写锁定。

  • 第三次单击:禁用大写锁定。

2 个答案:

答案 0 :(得分:0)

不幸的是,在ShiftHandler :: toggleShift内部实现了对Shift行为的处理,它在其中检查mouseDoubleClickInterval和其他内容,因此,实现您提到的功能的最简单方法是通过将Shift按钮与一个MouseArea。

要做的事情:

下面是一个如何使它具有以下功能的示例:

  • 单次单击时的Shift切换

  • capsLock在longPress上切换

代码:

shiftKeyPanel: KeyPanel {
    Rectangle {
        id: shiftKeyBackground
        radius: 5
        color: "#1e1b18"
        anchors.fill: parent
        anchors.margins: keyBackgroundMargin
        Image {
            id: shiftKeyIcon
            anchors.centerIn: parent
            sourceSize.width: 144 * keyIconScale
            sourceSize.height: 134 * keyIconScale
            smooth: false
            source: resourcePrefix + "images/shift-868482.svg"
        }
        states: [
            State {
                name: "capslock"
                when: InputContext.capsLock
                PropertyChanges {
                    target: shiftKeyBackground
                    color: "#5a892e"
                }
                PropertyChanges {
                    target: shiftKeyIcon
                    source: resourcePrefix + "images/shift-c5d6b6.svg"
                }
            },
            State {
                name: "shift"
                when: InputContext.shift
                PropertyChanges {
                    target: shiftKeyIcon
                    source: resourcePrefix + "images/shift-80c342.svg"
                }
            }
        ]

        MouseArea { ////////////////////here's the magic MouseArea
            anchors.fill: parent

            onClicked: {
                InputContext.capsLock = false
                InputContext.shiftHandler.toggleShift()
            }
            onPressAndHold: InputContext.capsLock = !InputContext.capsLock
        }
    }
    states: [
        State {
            name: "pressed"
            when: control.pressed
            PropertyChanges {
                target: shiftKeyBackground
                opacity: 0.80
            }
            PropertyChanges {
                target: shiftKeyIcon
                opacity: 0.6
            }
        },
        State {
            name: "disabled"
            when: !control.enabled
            PropertyChanges {
                target: shiftKeyBackground
                opacity: 0.8
            }
            PropertyChanges {
                target: shiftKeyIcon
                opacity: 0.2
            }
        }

答案 1 :(得分:0)

也许这段代码会给你一个想法

import QtQuick 2.0
import QtQuick.Controls 2.3
import QtQuick.VirtualKeyboard 2.1
import QtQuick.Window 2.0

Window {
    id: window
    width: 800
    height: 480
    color: "#F6F6F6"
    visible: true

    MouseArea  {
        id: content
        width: window.width

        Column {
            id: textEditors
            spacing: 15
            x: 12
            y: 12
            width: parent.width - 26

            Label {
                color: "#565758"
                text: "Tap fields to enter text"
                anchors.horizontalCenter: parent.horizontalCenter
            }
            TextField {
                width: parent.width
                placeholderText: "One line field"
                inputMethodHints: Qt.ImhPreferLowercase
            }
            TextField {
                id: passwordField
                width: parent.width
                echoMode: TextField.Password
                placeholderText: "Password field"
                // changes do not work
                inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhPreferLowercase | Qt.ImhSensitiveData | Qt.ImhNoPredictiveText

                onTextChanged: console.log(text)
            }
        }
    }

    InputPanel {
        id: inputPanel
        z: 2
        y: window.height
        width: window.width

        property bool shiftActive: (InputContext == null) ? null : InputContext.shiftActive
        property variant shiftKey: null

        function findKey(parent) {
            if (parent === null) {
                return null
            }

            var children = parent.children
            if (children === undefined || children === null) {
                return null
            }

            var obj = null

            for (var i = 0; i < children.length; i++) {
                obj = children[i]

                if (obj instanceof ShiftKey) {
                    return obj
                }

                obj = findKey(obj)

                if (obj === null) {
                    continue
                }

                if (obj instanceof ShiftKey) {
                    return obj
                }
            }

            return null
        }

        Timer {
            id: timer
            interval: 0
            repeat: false
            running: false
            onTriggered: {
                inputPanel.shiftKey.clicked()
            }
        }

        Connections {
            target: (InputContext.inputItem != null && InputContext.inputItem.echoMode === TextField.Password) ? InputContext.inputItem : null
            onTextChanged: {
                if (inputPanel.shiftActive) {
                    if (inputPanel.shiftKey == null) {
                        inputPanel.shiftKey = inputPanel.findKey(inputPanel.keyboard)
                    }

                    timer.start()
                }
            }
        }


        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
                }
            }
        }
    }
}