Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Reading Key Sequences from key event
Qt 6.11 is out! See what's new in the release blog

Qt Reading Key Sequences from key event

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DannyL408D Offline
    DannyL408D Offline
    DannyL408
    wrote on last edited by
    #1

    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.

    raven-worxR 1 Reply Last reply
    0
    • DannyL408D DannyL408

      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.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • DannyL408D Offline
        DannyL408D Offline
        DannyL408
        wrote on last edited by DannyL408
        #3

        @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)

        raven-worxR 1 Reply Last reply
        0
        • DannyL408D DannyL408

          @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)

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved