MultitapInputMethod.qml 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. import QtQuick
  4. import QtQuick.VirtualKeyboard as VKB
  5. VKB.InputMethod {
  6. property string multitapSequence
  7. property int multitapIndex: -1
  8. onMultitapSequenceChanged: selectionListChanged(VKB.SelectionListModel.Type.WordCandidateList)
  9. onMultitapIndexChanged: selectionListActiveItemChanged(VKB.SelectionListModel.Type.WordCandidateList, multitapIndex)
  10. property variant multiTapTimer: Timer {
  11. interval: 1200
  12. onTriggered: {
  13. update()
  14. }
  15. }
  16. function inputModes(locale) {
  17. return [VKB.InputEngine.InputMode.Latin, VKB.InputEngine.InputMode.Numeric, VKB.InputEngine.InputMode.Dialable];
  18. }
  19. function setInputMode(locale, inputMode) {
  20. return true
  21. }
  22. function setTextCase(textCase) {
  23. return true
  24. }
  25. function reset() {
  26. multiTapTimer.stop()
  27. multitapIndex = -1
  28. multitapSequence = ""
  29. }
  30. function update() {
  31. multiTapTimer.stop()
  32. multitapIndex = -1
  33. multitapSequence = ""
  34. if (inputContext !== null && inputContext.preeditText.length > 0) {
  35. inputContext.commit()
  36. }
  37. }
  38. function keyEvent(key, text, modifiers) {
  39. var accept = false
  40. switch (key) {
  41. case Qt.Key_Enter:
  42. case Qt.Key_Return:
  43. case Qt.Key_Tab:
  44. update()
  45. break
  46. case Qt.Key_Backspace:
  47. if (inputContext.preeditText.length > 0) {
  48. inputContext.clear()
  49. update()
  50. accept = true
  51. }
  52. break
  53. default:
  54. if (key !== inputEngine.previousKey) {
  55. update()
  56. }
  57. multitapSequence = text
  58. if (multitapSequence.length > 1) {
  59. multitapIndex = multiTapTimer.running ? (multitapIndex + 1) % multitapSequence.length : 0
  60. inputContext.preeditText = multitapSequence.charAt(multitapIndex)
  61. multiTapTimer.restart()
  62. } else {
  63. inputContext.commit(text)
  64. }
  65. accept = true
  66. break
  67. }
  68. return accept;
  69. }
  70. function selectionLists() {
  71. return [VKB.SelectionListModel.Type.WordCandidateList];
  72. }
  73. function selectionListItemCount(type) {
  74. return multitapSequence.length > 1 ? multitapSequence.length : 0
  75. }
  76. function selectionListData(type, index, role) {
  77. var result = null
  78. switch (role) {
  79. case VKB.SelectionListModel.Role.Display:
  80. result = multitapSequence.charAt(index)
  81. break
  82. default:
  83. break
  84. }
  85. return result
  86. }
  87. function selectionListItemSelected(type, index) {
  88. multitapIndex = index
  89. inputContext.preeditText = multitapSequence.charAt(multitapIndex)
  90. update()
  91. }
  92. }