Unsolved Keyboard Shortcuts implementation for QML
-
Hi all,
I am trying to implement keyboard shortcuts for the application developed in QML & Qt.
I want to clarify which way will be the better or doable.- Should I store all the shortcuts in a C++ class and expose it to QML
- Or should I directly implement the shortcuts directly in QML
Which method will be more efficient ?
Please do suggest.Thanks in advance
-
Both ways are OK. Choose whichever is easier / more elegant for you.
-
Thank you Sierdzio:)
-
Hi All,
I was working on detecting multiple keys pressed in QML
Here is what I am doingShortcut{
sequences:
{
///Here i need to handle sequence of keys pressed, for ex. 1 followed by 2
//Once both the keys are pressed , I need to handle it in onActivated slot.
}
onActivated: {
console.log("Comwheel press activated \n");
}How can I achieve this functionality.
Thanks in Advance -
Please use code tags, it makes the code much easier to read.
Also, please read the docs for Shortcut component, you're using it wrong.
If you really want key sequences (meaning: press Key1, press Key2, press Key3 and not hold Key1, hold Key2, hold Key3), then this component won't help you at all. You need to write some custom logic to remember a list of previous key presses, and if that list contains the combination you desire, then act upon it in a slot.
-
Hi Seirdzio,
Thanks for replyingI am sorry that i haven't stated the problem very precisely.
I was trying to detect multiple keys pressed inside Keys.onPressed slot
I don't want to detect individual keys pressed, but press and hold kind of behaviour detection
for .ex press and hold 2 + press and hold 1 + press 6
in this case i need to perform some action.
was trying to do the following wayitem{
id:appwindowproperty int a:0
property int b:0
property int c: 0Keys.onPressed: {
//console.log("Inside the on key pressed function \n")
console.log("Key Pressed :\n" + event.key)if(event.key == Qt.Key_2 || event.key == Qt.Key_1 || event.key == Qt.Key_5) { console.log("2 , 1 , or 6 is being pressed \n") if(appwindow.a == 0 ) { if(event.key == Qt.Key_2) { console.log(" 2 is pressed") appwindow.a = 2 console.log(appwindow.a) } } if(appwindow.a == 2) { if(event.key == Qt.Key_1) { appwindow.b = 1 }
else
{
c//reset a
}
}
if( a == 2 && b == 1)
{
if(event.key == Qt.Key_6)
{
appwindow.c = 6
}
else
{
//reset a & b to take new key presses
}
}
console.log("Key sequence" + appwindow.a
+ appwindow.b
+ appwindow.c)
}if(event.key == Qt.Key_C){ console.log("Key C is being Pressed\n") //Call some slot for individual key press }
}
But , As per my perception multiple key events cannot be handled over here.
Thanks in advance
-
Keys.onPressed
is invoked when a key (single key) is pressed. It is not invoked when the key is released, and it only reports single key press at a time. So, making this logic work would require you to also detect key releases and making sure that the keys are pressed simultaneously.Anyway, from what you write it seems that
Shortcut
component is exactly what you need, just use it properly (see the docs). -
Hi Seirdzio,
I tried using "shortcut " of QML
But I couldn't figure out how to use it exactly, and the documentation didn't help me at all.After scratching head for several hours ,
I could figure out the solutionHere it is,
property bool secondKeyPressed: false
property bool firstKeyPressed: false
property bool sixthKeyPressed: falseKeys.onPressed: { if( event.isAutoRepeat ) return; //console.log("Key Pressed\n") if( event.key == Qt.Key_2 ) { console.log("Key 2 is pressed\n"); view.secondKeyPressed = true ; } if( event.key == Qt.Key_1 ) { console.log("Key 1 is pressed\n"); view.firstKeyPressed = true ; } if(event.key === Qt.Key_6) { console.log("Key 6 is pressed\n"); view.sixthKeyPressed = true } if(view.firstKeyPressed && view.secondKeyPressed && view.sixthKeyPressed) { console.log("Key Sequence is 216"); } } Keys.onReleased: { if ( event.isAutoRepeat ) return ; if( event.key == Qt.Key_2 ) { console.log("Key 2 is released\n"); view.secondKeyPressed = false ; } if( event.key == Qt.Key_1 ) { console.log("Key 1 is released\n"); view.firstKeyPressed = false ; } if(event.key === Qt.Key_6) { console.log("Key 6 is released\n"); view.sixthKeyPressed = false } }