Qt Reading Key Sequences from key event



  • Hello I am trying to save key sequences such as (ctrl + alt + shift + key) or just a key or (ctrl + key) etc.

    using keyReleaseEvent i am able to sucessfully capture the combo but when i use control + key modifiers i get unicode(?) back when i return event->text() rather than the actual letter compared to all the other cases t hat dont involve control (shift + e) or (alt + e) prints out "Shift+E" and "Alt+e"but (control + e) prints out "Control+\u0005"

    what is going on here? am i trying to capture a key sequence that i want to save into a keysequence improperly? (i need the string to show properly as E so i can display the current keysequence of the shortcut as the text for the label.


  • Moderators

    @DannyL408
    Here is a code to convert a QKeyEvent to QKeySequence:

    QKeyEvent* ke;
    
    QString modifier;
    QString key;
    
    if (ke->modifiers() & Qt::ShiftModifier)
            modifier += "Shift+";
    if (ke->modifiers() & Qt::ControlModifier)
    	modifier += "Ctrl+";
    if (ke->modifiers() & Qt::AltModifier)
    	modifier += "Alt+";
    if (ke->modifiers() & Qt::MetaModifier)
    	modifier += "Meta+";
    
    key = QKeySequence(ke->key()).toString();
    
    QKeySequence ks(modifier + key);
    

    Note that there is also a QKeySequenceEdit widget



  • @raven-worx said in Qt Reading Key Sequences from key event:

    @DannyL408
    Here is a code to convert a QKeyEvent to QKeySequence:

    QKeyEvent* ke;
    
    QString modifier;
    QString key;
    
    if (ke->modifiers() & Qt::ShiftModifier)
            modifier += "Shift+";
    if (ke->modifiers() & Qt::ControlModifier)
    	modifier += "Ctrl+";
    if (ke->modifiers() & Qt::AltModifier)
    	modifier += "Alt+";
    if (ke->modifiers() & Qt::MetaModifier)
    	modifier += "Meta+";
    
    key = QKeySequence(ke->key()).toString();
    
    QKeySequence ks(modifier + key);
    

    Note that there is also a QKeySequenceEdit widget

    thank you!!
    i was pulling my hair out trying to figure it out
    i had to use keyRELEASEevent over keyPRESSevent to make it detect the full combo. i will look into the widget

    @raven-worx one thing i noticed about QKeySequenceEdit is that if i hold down a key like "R" it will show up buggy in the widget R+R+R+R when i just want R. is there any way to filter it to just modifiers + a key and no dupes ( since that isnt a real key sequence, and invalidate keys without a key such as just pressing control without a key)


  • Moderators

    @DannyL408
    haven't tried it, but you have a keySequenceChanged() signal. Check the seuqence in a slot connected to it and change the sequence if it's not desirable.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.