1 Star 1 Fork 8

圆月庄主/QmlInputMethod

forked from LukeLee/QmlInputMethod 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
VirtualKeyboard.qml 21.84 KB
一键复制 编辑 原始数据 按行查看 历史
MrEO 提交于 2018-04-20 10:41 . fixed a drop bug
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtTest 1.0
import InputMethod 1.0
Item {
id: virtualKeyboardRoot
focus: false
property int keySpacing: 8
property int keyButtonWidth: (width - keySpacing*11) / 10
property int keyButtonHeight: (keyboardHeight - keySpacing * 4) / 4
property int keyTextFontSize: 30
property int shiftKeyTextFontSize: 15
property int keyboardHeight: height / 2
property string color: "AliceBlue"
property int defaultRootY: 0
property bool active: false
property bool capsLock: false
property bool shift: false
property bool raise: false
property bool chinese: true
property var root
property var window
Rectangle {
id: coverItem
anchors.left: virtualKeyboardRec.left
anchors.right: virtualKeyboardRec.right
height: 0
anchors.bottom: virtualKeyboardRec.top
color: virtualKeyboardRec.color
MouseArea {
anchors.fill: parent
}
}
Rectangle {
id: virtualKeyboardRec
anchors.left: parent.left
anchors.right: parent.right
height: keyboardHeight
y : parent.height
color: parent.color
MouseArea {
anchors.fill: parent
}
Column {
id: generalKeyboard
anchors.centerIn: parent
spacing: keySpacing
Row {
id: row1
anchors.horizontalCenter: parent.horizontalCenter
spacing: keySpacing
Repeater {
model: ListModel {
ListElement {key_text: "Q"; key_value : Qt.Key_Q; shift_key_text: "1"; shift_key_value: Qt.Key_1}
ListElement {key_text: "W"; key_value : Qt.Key_W; shift_key_text: "2"; shift_key_value: Qt.Key_2}
ListElement {key_text: "E"; key_value : Qt.Key_E; shift_key_text: "3"; shift_key_value: Qt.Key_3}
ListElement {key_text: "R"; key_value : Qt.Key_R; shift_key_text: "4"; shift_key_value: Qt.Key_4}
ListElement {key_text: "T"; key_value : Qt.Key_T; shift_key_text: "5"; shift_key_value: Qt.Key_5}
ListElement {key_text: "Y"; key_value : Qt.Key_Y; shift_key_text: "6"; shift_key_value: Qt.Key_6}
ListElement {key_text: "U"; key_value : Qt.Key_U; shift_key_text: "7"; shift_key_value: Qt.Key_7}
ListElement {key_text: "I"; key_value : Qt.Key_I; shift_key_text: "8"; shift_key_value: Qt.Key_8}
ListElement {key_text: "O"; key_value : Qt.Key_O; shift_key_text: "9"; shift_key_value: Qt.Key_9}
ListElement {key_text: "P"; key_value : Qt.Key_P; shift_key_text: "0"; shift_key_value: Qt.Key_0}
}
delegate: keyButton
}
}
Row {
id: row2
anchors.horizontalCenter: parent.horizontalCenter
spacing: keySpacing
Repeater {
model: ListModel {
ListElement {key_text: "A"; key_value : Qt.Key_A; shift_key_text: "~"; shift_key_value: Qt.Key_AsciiTilde}
ListElement {key_text: "S"; key_value : Qt.Key_S; shift_key_text: "!"; shift_key_value: Qt.Key_Exclam}
ListElement {key_text: "D"; key_value : Qt.Key_D; shift_key_text: "@"; shift_key_value: Qt.Key_At}
ListElement {key_text: "F"; key_value : Qt.Key_F; shift_key_text: "#"; shift_key_value: Qt.Key_NumberSign}
ListElement {key_text: "G"; key_value : Qt.Key_G; shift_key_text: "%"; shift_key_value: Qt.Key_Percent}
ListElement {key_text: "H"; key_value : Qt.Key_H; shift_key_text: "+"; shift_key_value: Qt.Key_Plus}
ListElement {key_text: "J"; key_value : Qt.Key_J; shift_key_text: "*"; shift_key_value: Qt.Key_multiply}
ListElement {key_text: "K"; key_value : Qt.Key_K; shift_key_text: "("; shift_key_value: Qt.Key_ParenLeft}
ListElement {key_text: "L"; key_value : Qt.Key_L; shift_key_text: ")"; shift_key_value: Qt.Key_ParenRight}
}
delegate: keyButton
}
}
Row {
id: row3
anchors.horizontalCenter: parent.horizontalCenter
spacing: keySpacing
ItemDelegate {
id: capsLockButton
width: keyButtonWidth
height: keyButtonHeight
Image {
id: capsLockImage
visible: false
anchors.centerIn: parent
height: keyButtonHeight
width: height
source: capsLock ? "icons/capslock_on.png" : "icons/capslock_off.png"
}
Text {
id: apostropheText
anchors.centerIn: parent
font.pixelSize: keyTextFontSize
text: ""
}
property int keyValue: Qt.Key_Apostrophe
states: [
State {
name: "capsLock"
when: !chinese
PropertyChanges {
target: capsLockImage
visible: true
}
PropertyChanges {
target: apostropheText
visible: false
}
PropertyChanges {
target: capsLockButton
keyValue: Qt.Key_CapsLock
}
}
]
onClicked: {
keySimulator.keyClick(keyValue, Qt.NoModifier, -1)
if (keyValue == Qt.Key_CapsLock) {
capsLock = !capsLock
}
}
}
Repeater {
id: row3Rep
model: ListModel {
ListElement {key_text: "Z"; key_value : Qt.Key_Z; shift_key_text: "\""; shift_key_value: Qt.Key_QuoteDbl}
ListElement {key_text: "X"; key_value : Qt.Key_X; shift_key_text: "/"; shift_key_value: Qt.Key_Slash}
ListElement {key_text: "C"; key_value : Qt.Key_C; shift_key_text: "-"; shift_key_value: Qt.Key_Minus}
ListElement {key_text: "V"; key_value : Qt.Key_V; shift_key_text: "_"; shift_key_value: Qt.Key_Underscore}
ListElement {key_text: "B"; key_value : Qt.Key_B; shift_key_text: ":"; shift_key_value: Qt.Key_Colon}
ListElement {key_text: "N"; key_value : Qt.Key_N; shift_key_text: ";"; shift_key_value: Qt.Key_Semicolon}
ListElement {key_text: "M"; key_value : Qt.Key_M; shift_key_text: "?"; shift_key_value: Qt.Key_Question}
}
delegate: keyButton
}
Loader {
id: normalBackspaceButton
width: keyButtonWidth * 2
height: keyButtonHeight
sourceComponent: backspaceButton
}
}
Row {
id: row4
anchors.horizontalCenter: parent.horizontalCenter
spacing: keySpacing
ItemDelegate {
id: symbolButton
Text {
anchors.centerIn: parent
text: "123"
font.pixelSize: keyTextFontSize
}
width: keyButtonWidth * 2
height: keyButtonHeight
onClicked: {
shift = !shift
}
}
// Loader {
// sourceComponent: keyButton
// onLoaded: {
// item.general_text = "分"
// item.general_value = Qt.Key_Apostrophe
// item.shift_text = "'"
// item.shift_value = Qt.Key_Apostrophe
// }
// }
ItemDelegate {
id: languageButton
width: keyButtonWidth
height: keyButtonHeight
readonly property string text1: ""
readonly property string text2: "En"
Text {
id: languageText1
anchors.verticalCenter: parent.verticalCenter
anchors.rightMargin: -10
anchors.right: parent.horizontalCenter
text: languageButton.text1
font.pixelSize: keyTextFontSize
}
Text {
id: languageText2
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.horizontalCenter
anchors.margins: 10
text: languageButton.text2
font.pixelSize: shiftKeyTextFontSize
}
states: [
State {
name: "English"
when: !chinese
PropertyChanges {
target: languageText1
text: languageButton.text2
}
PropertyChanges {
target: languageText2
text: languageButton.text1
}
}
]
onClicked: {
chinese = !chinese
keySimulator.keyClick(Qt.Key_Space, Qt.ControlModifier, -1)
}
}
Loader {
id: commaButton
sourceComponent: keyButton
onLoaded: {
item.general_text = ""
item.general_value = Qt.Key_Comma
item.shift_text = ","
item.shift_value = Qt.Key_Comma
}
}
ItemDelegate {
id: spaceButton
height: keyButtonHeight
width: keyButtonWidth * 3
Image {
height: parent.height
width: height
anchors.centerIn: parent
source: "icons/space.png"
}
onClicked: {
keySimulator.keyClick(Qt.Key_Space, Qt.NoModifier, -1)
}
}
Loader {
id: periodButton
sourceComponent: keyButton
onLoaded: {
item.general_text = ""
item.general_value = Qt.Key_Period
item.shift_text = "."
item.shift_value = Qt.Key_Period
}
}
ItemDelegate {
id: putawayButton
width: keyButtonWidth
height: keyButtonHeight
Image {
height: parent.height
width: height
anchors.centerIn: parent
source: "icons/keyboard.png"
}
onClicked: {
rootDrop.start()
keySimulator.keyClick(Qt.Key_Escape, Qt.NoModifier, -1)
forceActiveFocus()
}
}
ItemDelegate {
id: tabButton
width: keyButtonWidth * 2
height: keyButtonHeight
Image {
height: parent.height
width: height
anchors.centerIn: parent
source: "icons/enter.png"
}
onClicked: {
keySimulator.keyClick(Qt.Key_Tab, Qt.NoModifier, -1)
}
}
}
}
Column {
id: digitsKeyboard
anchors.centerIn: parent
spacing: keySpacing
visible: false
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: keySpacing
Repeater {
model: ListModel {
ListElement {key_text: "1"; key_value: Qt.Key_1}
ListElement {key_text: "2"; key_value: Qt.Key_2}
ListElement {key_text: "3"; key_value: Qt.Key_3}
ListElement {key_text: "4"; key_value: Qt.Key_4}
ListElement {key_text: "5"; key_value: Qt.Key_5}
ListElement {key_text: "6"; key_value: Qt.Key_6}
ListElement {key_text: "7"; key_value: Qt.Key_7}
ListElement {key_text: "8"; key_value: Qt.Key_8}
ListElement {key_text: "9"; key_value: Qt.Key_9}
ListElement {key_text: "0"; key_value: Qt.Key_0}
}
delegate: singleKeyButton
}
}
Item {
width: 1
height: keyButtonHeight
}
Row {
anchors.right: parent.right
Loader {
id: digitsBackspaceButton
width: keyButtonWidth * 2
height: keyButtonHeight
sourceComponent: backspaceButton
}
}
Row {
anchors.right: parent.right
ItemDelegate {
width: keyButtonWidth
height: keyButtonHeight
Text {
id: generalText
anchors.centerIn: parent
font.pixelSize: keyTextFontSize
text: "."
}
onClicked: {
keySimulator.keyClick(Qt.Key_Period, Qt.NoModifier, -1)
}
}
ItemDelegate {
id: digitsPutawayButton
width: keyButtonWidth
height: keyButtonHeight
Image {
height: parent.height
width: height
anchors.centerIn: parent
source: "icons/keyboard.png"
}
onClicked: {
rootDrop.start()
keySimulator.keyClick(Qt.Key_Escape, Qt.NoModifier, -1)
forceActiveFocus()
}
}
ItemDelegate {
id: digitsTabButton
width: keyButtonWidth * 2
height: keyButtonHeight
Image {
height: parent.height
width: height
anchors.centerIn: parent
source: "icons/enter.png"
}
onClicked: {
keySimulator.keyClick(Qt.Key_Tab, Qt.NoModifier, -1)
}
}
}
}
}
TestEvent {
id: keySimulator
}
Component {
id: keyButton
ItemDelegate {
id: keyButtonRoot
width: keyButtonWidth
height: keyButtonHeight
property string general_text: key_text
property int general_value: key_value
property string shift_text: shift_key_text
property int shift_value: shift_key_value
Text {
id: generalText
anchors.centerIn: parent
font.pixelSize: keyTextFontSize
text: general_text
}
Text {
id: shiftText
anchors.top: parent.top
anchors.right: parent.right
anchors.margins: 10
text: shift_text
font.pixelSize: shiftKeyTextFontSize
}
Connections {
target: virtualKeyboardRoot
onShiftChanged: {
var temp = keyButtonRoot.general_text
keyButtonRoot.general_text = keyButtonRoot.shift_text
keyButtonRoot.shift_text = temp
temp = keyButtonRoot.general_value
keyButtonRoot.general_value = keyButtonRoot.shift_value
keyButtonRoot.shift_value = temp
}
}
onClicked: {
//TestEvent仅使用Qt.ShiftModifier无法输入大写字母
if (capsLock) {
helper.keyClickUpper(general_value);
}
else {
keySimulator.keyClick(general_value, Qt.NoModifier, -1)
}
}
onPressAndHold: {
keySimulator.keyClick(shift_value, Qt.NoModifier, -1)
}
}
}
Component {
id: singleKeyButton
ItemDelegate {
id: singleKeyButtonRoot
width: keyButtonWidth
height: keyButtonHeight
property string general_text: key_text
property int general_value: key_value
Text {
id: generalText
anchors.centerIn: parent
font.pixelSize: keyTextFontSize
text: general_text
}
onClicked: {
keySimulator.keyClick(general_value, Qt.NoModifier, -1)
}
}
}
Component {
id: backspaceButton
ItemDelegate {
id: backspaceButtonRoot
width: keyButtonWidth * 2
height: keyButtonHeight
Image {
anchors.centerIn: parent
height: keyButtonHeight
width: height
source: "icons/backspace.png"
}
Timer {
id: backspaceTimer
repeat: true
interval: 50
onTriggered: {
keySimulator.keyClick(Qt.Key_Backspace, Qt.NoModifier, -1)
}
}
onClicked: {
keySimulator.keyClick(Qt.Key_Backspace, Qt.NoModifier, -1)
}
onPressAndHold: {
backspaceTimer.start()
}
onReleased: {
backspaceTimer.stop()
}
}
}
SequentialAnimation {
id: rootRaise
property int y: root.y
PropertyAnimation { target: root; properties: "y"; to: rootRaise.y; duration: 200; easing.type: Easing.InOutQuad; }
}
SequentialAnimation {
id: rootDrop
PropertyAnimation { target: root; properties: "y"; to: defaultRootY; duration: 200; easing.type: Easing.InOutQuad; }
}
function call(activeFocusControl, offset) {
shift = false
//判断是否调出虚拟键盘
if (Input.classIsListen(helper.getClassName(activeFocusControl), true)
&& !activeFocusControl.readOnly) {
state = "visible"
active = true
//判断虚拟键盘遮挡
if ((helper.getAbsolutePos(activeFocusControl).y - root.y + activeFocusControl.height)
> (height - keyboardHeight - offset)) {
var absPos = helper.getAbsolutePos(activeFocusControl).y - root.y
rootRaise.y = window.height - keyboardHeight
- absPos - activeFocusControl.height
- offset
rootRaise.start()
raise = true
coverItem.height = window.height - (window.height + rootRaise.y + virtualKeyboard.item.keyboardHeight)
}
else {
raise = false
coverItem.height = 0
}
///判断是否为仅数字
if (activeFocusControl.inputMethodHints & Qt.ImhDigitsOnly) {
state = "digits"
}
}
else {
if (raise) {
rootDrop.start()
}
coverItem.height = 0
raise = false
active = false
state = ""
}
}
states: [
State {
name: "visible"
PropertyChanges {
target: virtualKeyboardRec
y: virtualKeyboardRoot.height - virtualKeyboardRec.height
}
},
State {
name: "digits"
extend: "visible"
PropertyChanges { target: generalKeyboard; visible: false }
PropertyChanges { target: digitsKeyboard; visible: true }
}
]
transitions: Transition {
from: ""
reversible: true
ParallelAnimation {
NumberAnimation {
properties: "y"
duration: 250
easing.type: Easing.InOutQuad
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yanshinghwang/QmlInputMethod.git
git@gitee.com:yanshinghwang/QmlInputMethod.git
yanshinghwang
QmlInputMethod
QmlInputMethod
master

搜索帮助