Advertisement
Guest User

Untitled

a guest
Nov 28th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 3.79 KB | None | 0 0
  1. import QtQuick 2.9
  2. import QtQuick.Controls 2.4
  3.  
  4. Item {
  5.     id: root
  6.     width: 90
  7.     height: 30
  8.     signal accepted
  9.     property string keyName
  10.     property string text: "Binded: "+keyName
  11.     property bool isListening: false
  12.     property color fontColor1: "#000000"
  13.     property color fontColor2: "#9B1B03"
  14.     function askForKey(){
  15.         if(!root.isListening){
  16.             txt.text = "Press Key"
  17.         }
  18.         root.isListening = true
  19.         root.focus = true
  20.     }
  21.     Timer {
  22.         id: focusTimer
  23.         interval: 500; running: root.focus; repeat: true
  24.         onTriggered: {
  25.             if(txt.color == root.fontColor1){
  26.                 txt.color = root.fontColor2
  27.             }else{
  28.                 txt.color = root.fontColor1
  29.             }
  30.         }
  31.     }
  32.  
  33.     Keys.onPressed: {
  34.         if(root.isListening){
  35.             switch(event.key){
  36.             case 16777251:
  37.                 root.keyName="ALT";
  38.                 break;
  39.             case 16777264:
  40.                 root.keyName="F1";
  41.                 break;
  42.             case 16777265:
  43.                 root.keyName="F2";
  44.                 break;
  45.             case 16777266:
  46.                 root.keyName="F3";
  47.                 break;
  48.             case 16777267:
  49.                 root.keyName="F4";
  50.                 break;
  51.             case 16777268:
  52.                 root.keyName="F5";
  53.                 break;
  54.             case 16777269:
  55.                 root.keyName="F6";
  56.                 break;
  57.             case 16777270:
  58.                 root.keyName="F7";
  59.                 break;
  60.             case 16777271:
  61.                 root.keyName="F8";
  62.                 break;
  63.             case 16777272:
  64.                 root.keyName="F9";
  65.                 break;
  66.             case 16777273:
  67.                 root.keyName="F10";
  68.                 break;
  69.             case 16777274:
  70.                 root.keyName="F11";
  71.                 break;
  72.             case 16777275:
  73.                 root.keyName="F12";
  74.                 break;
  75.             case 32:
  76.                 root.keyName="SPACE";
  77.                 break;
  78.             case 16777234:
  79.                 root.keyName="←";
  80.                 break;
  81.             case 16777235:
  82.                 root.keyName="↑";
  83.                 break;
  84.             case 16777236:
  85.                 root.keyName="→";
  86.                 break;
  87.             case 16777237:
  88.                 root.keyName="↓";
  89.                 break;
  90.             default:
  91.                 root.keyName=event.text.toUpperCase();
  92.                 break;
  93.             }
  94.             txt.text = "Binded: "+root.keyName
  95.             root.isListening = false
  96.             root.focus = false
  97.             txt.color = root.fontColor1
  98.             root.accepted()
  99.         }
  100.     }
  101.     FontLoader {
  102.         id: roboto
  103.         source: "../assets/fonts/AdobeClean-Regular.ttf"
  104.     }
  105.  
  106.     Rectangle {
  107.         id: rectangle
  108.         color: "#d1d1d1"
  109.         anchors.fill: parent
  110.  
  111.         Text {
  112.             id: txt
  113.             width: root.width
  114.             font.family: roboto.name
  115.             height: root.height
  116.             color: root.fontColor1
  117.             text: root.text
  118.             anchors.horizontalCenter: parent.horizontalCenter
  119.             anchors.verticalCenter: parent.verticalCenter
  120.             font.pixelSize: 12
  121.             verticalAlignment: Text.AlignVCenter
  122.             horizontalAlignment: Text.AlignHCenter
  123.         }
  124.     }
  125.     MouseArea {
  126.         anchors.fill: parent
  127.         hoverEnabled: true
  128.         onEntered: {
  129.             if(!root.focus){
  130.                 txt.color = root.fontColor2
  131.             }
  132.         }
  133.         onExited: {
  134.             if(!root.focus){
  135.                 txt.color = root.fontColor1
  136.             }
  137.         }
  138.         onClicked: {
  139.             root.askForKey()
  140.         }
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement