1
0

KeyButton.qml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import QtQuick 2.0
  2. import FreeVirtualKeyboard 1.0
  3. /**
  4. * This is the type implements one single key button in the InputPanel
  5. * The code has been copied from
  6. * http://tolszak-dev.blogspot.de/2013/04/qplatforminputcontext-and-virtual.html
  7. */
  8. Item {
  9. id:root
  10. // The background color of this button
  11. property color color: "#35322f"
  12. // The key text to show in this button
  13. property string text
  14. // The font for rendering of text
  15. property alias font: txt.font
  16. // The color of the text in this button
  17. property alias textColor: txt.color
  18. // This property holds the pressed status of the key.
  19. property alias isPressed: buttonMouseArea.pressed
  20. // This property holds a reference to the input panel.
  21. // A key can only show the charcter preview popup if this property contains
  22. // a valid refernce to the input panel
  23. property var inputPanel
  24. // This property holds the highlighted status of the key
  25. // The highlighted status is a little bit different from the pressed status
  26. // If the user releases the key, it is not pressed anymore, but it is still
  27. // highlighted for some milliseconds
  28. property bool isHighlighted: false
  29. // Sets the show preview attribute for the character preview key popup
  30. property bool showPreview: true
  31. // Sets the key repeat attribute.
  32. // If the repeat is enabled, the key will repeat the input events while held down.
  33. // The default is false.
  34. property bool repeat: false
  35. // Sets the key code for input method processing.
  36. property int key
  37. // Sets the display text - this string is rendered in the keyboard layout.
  38. // The default value is the key text.
  39. property alias displayText: txt.text
  40. // Sets the function key attribute.
  41. property bool functionKey: false
  42. signal clicked()
  43. signal pressed()
  44. signal released()
  45. Rectangle {
  46. anchors.fill: parent
  47. radius: height / 20
  48. color: isHighlighted ? Qt.tint(root.color, "#801e6fa7") : root.color
  49. Text {
  50. id: txt
  51. color: "white"
  52. anchors.margins: parent.height / 6
  53. anchors.fill: parent
  54. fontSizeMode: Text.Fit
  55. font.pixelSize: height
  56. horizontalAlignment: Text.AlignHCenter
  57. verticalAlignment: Text.AlignVCenter
  58. text: root.text
  59. }
  60. MouseArea {
  61. id: buttonMouseArea
  62. anchors.fill: parent
  63. onClicked: root.clicked()
  64. onPressed: root.pressed()
  65. onReleased: root.released()
  66. }
  67. }
  68. Timer {
  69. id: highlightTimer
  70. interval: 100
  71. running: !isPressed
  72. repeat: false
  73. onTriggered: {
  74. isHighlighted = false;
  75. }
  76. }
  77. Timer {
  78. id: repeatTimer
  79. interval: 500
  80. repeat: true
  81. running: root.repeat && root.isPressed
  82. onTriggered: {
  83. if (root.state == "")
  84. {
  85. root.state = "REPEATING"
  86. console.log("switching to repeating");
  87. }
  88. else if (root.state == "REPEATING")
  89. {
  90. console.log("repeating");
  91. }
  92. if (!functionKey)
  93. {
  94. InputEngine.sendKeyToFocusItem(text)
  95. }
  96. }
  97. }
  98. // If the InputPanel property has a valid InputPanel reference and if
  99. // showPreview is true, then this function calls showKeyPopup() to
  100. // show the character preview popup.
  101. onPressed: {
  102. if (inputPanel != null && showPreview)
  103. {
  104. inputPanel.showKeyPopup(root);
  105. }
  106. isHighlighted = true;
  107. }
  108. onReleased: {
  109. state = ""
  110. console.log("onReleased - functionKey = " + functionKey)
  111. if (!functionKey)
  112. {
  113. InputEngine.sendKeyToFocusItem(text)
  114. }
  115. }
  116. states: [
  117. State {
  118. name: "REPEATING"
  119. PropertyChanges { target: repeatTimer; interval: 50}
  120. }
  121. ]
  122. }