View Full Version : QKeySequence - get each QKey
thomaspu
17th July 2008, 02:39
I've got a QKeySequence and I want to get each individual QKey... I've been messing with a few ideas, but everything I've come up with seems really messy.
I need the QKeySequence because I'm trying to setup a windows global hotkey for my application using the windows RegisterHotKey function. I know that I can test the Ctrl, Alt, Shift and Windows key modifiers with the QKeySequence->matches( ...) function. But how do I go about getting the main key from a "Ctrl+Shift+F10" key sequence?
Thanks,
Paul
triperzonak
17th July 2008, 12:23
did you try using QWidget::grabKeyboard ()?
http://doc.trolltech.com/4.4/qwidget.html#grabKeyboard
:)
thomaspu
17th July 2008, 13:03
triperzonak: that QWidget:: grabKeyboard() is for getting the keys pressed. I already have the keys in my QKeySequence. How do I get each individual key out of the QKeySequence?
Paul
triperzonak
17th July 2008, 13:52
then its a matter of string manipulation..
since you can transfer your sequence to Qstring
QString QKeySequence::toString ( SequenceFormat format = PortableText )
and you know that it will be seperated by + sign and , (comma)
http://doc.trolltech.com/4.4/qkeysequence.html#toString
try playing with it like
QStringList keys= strsequence.split(",") or QStringList keys= strsequence.split("+") ;
do this by conditions
http://doc.trolltech.com/4.4/qstring.html#split
the keys then will be listed to QStringList
or if you know that their will be only 1 combination like CTR+ALT+A
you can use key= strsequence.right(strsequence.lastIndexOf("+"));
and many more solutions just play with it..
http://doc.trolltech.com/4.4/qstring.html#QString
thomaspu
17th July 2008, 15:30
I've already been down that route of using the QKeySequence toString method. Sure, it comes out in a format like Ctrl+Shift+ScrollLock but that doesn't transfer over easily into the corresponding Qt::key_scrollLock. Unless I do a gigantic bunch of if statements...
QString key = "ScrollLock"; //for example sake
if ( key == "A")
return Qt::Key_A;
if ( key == "B")
return Qt::Key_B;
...
if ( key == "F1")
return Qt::Key_F1;
...
if ( key == "ScrollLock")
return Qt::Key_scrollLock;
That really seems a bit tedious and messy, cause I would need to do that for each and every possible key the user could press. What I'm hunting for is a quicker way. I wonder if I could do something like:
QKeySequence seq = QKeySequence( "Ctrl+Shift+ScrollLock");
int modifiers = 0xFF00;
modifiers &= seq; //mask out modifiers
int qKey = 0x00FF;
qKey &= seq;
Ultimately for the RegisterHotKey() I need to translate the QKeySequence into modifiers (that part is easy) and into the individual keys as virtual keys (the MS way).
Paul
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.