【问题标题】:How change QT Virtual Keyboard Shift Logic如何改变 QT Virtual Keyboard Shift Logic
【发布时间】:2019-07-30 15:48:21
【问题描述】:

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

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

如何更改实现以在基于循环的程序上工作?

例如:

  • 拳头点击:为第一个字符输入启用大写字母。

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

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

【问题讨论】:

    标签: qt qml virtual-keyboard


    【解决方案1】:

    不幸的是,Shift 行为的处理是在 ShiftHandler::toggleShift 内部实现的,它检查 mouseDoubleClickInterval 和其他东西,所以完成你提到的功能的最简单方法是通过覆盖 shift 按钮以有点“hacky”的方式来完成它鼠标区域。

    要做的事情:

    下面是一个关于如何让它发挥作用的例子:

    • 单次点击切换切换

    • 长按时大写锁定切换

    代码:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢,“inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhPreferLowercase | Qt.ImhSensitiveData | Qt.ImhNoPredictiveText”是我一直在寻找的,它有帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2013-08-27
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多