Finally! Shortcuts with mouse support!
A while ago there was stuff going on in Qt to extend the current QKeySequence class to include mouse support as well. That would make it trivial for you as a user to create a shortcut like “Ctrl+LeftButton” (LeftButton would be the left mouse button). Sadly, that didn’t happen due to numerous difficulties.
But i was still left with the issue that i want to have shortcuts with potential mouse support so i decided to tidy up a class – named Shortcut – i already had for shortcuts in QML [1, 2] and extend it to have support for keyboard keys + mouse buttons. You can find the code in my “kdirchainrebuild” gitorious repo [1, 2].
If you expose this class to QML then you can use shortcuts on an application wide basis with code like the following snippet:
keys: [ "BackButton", "Alt+Left", "Backspace" ]
console.log("JS: " + keys + " pressed.")
In that snippet you already see mouse and keyboards keys/buttons. Not mixed though.
– (mouse) BackButton
– (keyboard) Alt+Left
– (keyboard) Backspace
which in my case are all used for going back (previous page) in Accretion.
The activated signal (onActivated slot) will then be emitted once (one of the) shortcut sequences has been triggered.
So there you have it. More advanced shortcut handling in a quite small package. Ready for you to use in QML and C++. I kinda hope that this class will find it’s way into plasma desktop 2 for the shortcut handling since that will both allow for more then 2 shortcuts per action and having shortcuts that include mouse buttons.
I do have some possible features where i’m not quite sure if i want to have them in this class or in a more advanced version.
– A flag field allowing you to tell which signals should be emitted. Right now the “activated” signal is emitted once the sequence is “pressed” but there could be value in having a signal for the “release” as well.
– The internal hash for mouse buttons could be made more memory efficient sine this will be one instance per shortcut object.
– I am using a QKeySequence internally because that is the only way i could get this working. It would be ideal if i can get the actual keys from QKeySequence as they are in the Qt::Key enum. This likely requires a change in Qt upstream (in QKeySequence) which isn’t really worth it.
I don’t know if/when i will implement any of the above. For now it works very well as it is :)