Results 1 to 5 of 5

Thread: QKeySequence - get each QKey

  1. #1
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QKeySequence - get each QKey

    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

  2. #2
    Join Date
    Dec 2007
    Posts
    129
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QKeySequence - get each QKey

    did you try using QWidget::grabKeyboard ()?

    http://doc.trolltech.com/4.4/qwidget.html#grabKeyboard


  3. #3
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QKeySequence - get each QKey

    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

  4. #4
    Join Date
    Dec 2007
    Posts
    129
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QKeySequence - get each QKey

    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

  5. #5
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QKeySequence - get each QKey

    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...
    Qt Code:
    1. QString key = "ScrollLock"; //for example sake
    2. if ( key == "A")
    3. return Qt::Key_A;
    4. if ( key == "B")
    5. return Qt::Key_B;
    6. ...
    7. if ( key == "F1")
    8. return Qt::Key_F1;
    9. ...
    10. if ( key == "ScrollLock")
    11. return Qt::Key_scrollLock;
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QKeySequence seq = QKeySequence( "Ctrl+Shift+ScrollLock");
    2. int modifiers = 0xFF00;
    3. modifiers &= seq; //mask out modifiers
    4. int qKey = 0x00FF;
    5. qKey &= seq;
    To copy to clipboard, switch view to plain text mode 

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.