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. Is there a way to detect pressed key combo without altering the original key without modifiers?

Is there a way to detect pressed key combo without altering the original key without modifiers?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 2.8k Views
  • 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.
  • L Offline
    L Offline
    lachdanan
    wrote on last edited by
    #1

    Hi,

    So basically if I press 1, I get uievent.key as 1. If I hold Ctrl, I get "Ctrl+1", if I hold shift, I get "Shift+!". That makes my hotkey system complicated.

    I allow users to define hotkeys per original key like 1, and add more customization based modifiers. Is it possible to get the actual key without the Shift modifying it?

    Is there a way to access this info, perhaps asking qt what key press would be sent if I didnt held down Shift?

    Thanks a lot.

    Pl45m4P 1 Reply Last reply
    0
    • L lachdanan

      Hi,

      So basically if I press 1, I get uievent.key as 1. If I hold Ctrl, I get "Ctrl+1", if I hold shift, I get "Shift+!". That makes my hotkey system complicated.

      I allow users to define hotkeys per original key like 1, and add more customization based modifiers. Is it possible to get the actual key without the Shift modifying it?

      Is there a way to access this info, perhaps asking qt what key press would be sent if I didnt held down Shift?

      Thanks a lot.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @lachdanan

      So you want to define a key combo, where it ignores the modifiers?!

      Have a look at this (https://doc.qt.io/qt-5/qkeyevent.html#modifiers).
      You could write an EventFilter to get the "modified" key.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lachdanan
        wrote on last edited by
        #3

        @Pl45m4 said in Is there a way to detect pressed key combo without altering the original key without modifiers?:

        modifiers

        Yes basically the app I am using passes me the key that was pressed but also the state of modifiers and mouse buttons. That's easy to get. But when I ask what key was pressed it not only includes the modifier keys in the string, but also with Shift, it alters the key that was pressed.

        So Shift + 1 becomes, Shift+!, but I want to only get 1, no matter the modifiers so I can respond based on my query of the modifier keys, but I don't want to handle shift differently. My code logic is like this:

        If key == 1
        if ctrl is pressed ...
        if shift is pressed ...

        But if shift alters the key string, now I have to handle ! etc too, but also every region will have different variations.

        I don't want to do that.

        L 1 Reply Last reply
        0
        • L lachdanan

          @Pl45m4 said in Is there a way to detect pressed key combo without altering the original key without modifiers?:

          modifiers

          Yes basically the app I am using passes me the key that was pressed but also the state of modifiers and mouse buttons. That's easy to get. But when I ask what key was pressed it not only includes the modifier keys in the string, but also with Shift, it alters the key that was pressed.

          So Shift + 1 becomes, Shift+!, but I want to only get 1, no matter the modifiers so I can respond based on my query of the modifier keys, but I don't want to handle shift differently. My code logic is like this:

          If key == 1
          if ctrl is pressed ...
          if shift is pressed ...

          But if shift alters the key string, now I have to handle ! etc too, but also every region will have different variations.

          I don't want to do that.

          L Offline
          L Offline
          lachdanan
          wrote on last edited by
          #4

          If you check out the way key codes are handled in .NET you can see it returns the key itself separately from the modifiers:

          https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keyeventargs.keycode?view=netframework-4.8

          private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
          {
              // Determine whether the key entered is the F1 key. If it is, display Help.
              if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
              {
                  // Display a pop-up Help topic to assist the user.
                  Help.ShowPopup(textBox1, "Enter your name.", new Point(textBox1.Bottom, textBox1.Right));
              }
              else if(e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt)
              {
                  // Display a pop-up Help topic to provide additional assistance to the user.
                  Help.ShowPopup(textBox1, "Enter your first name followed by your last name. Middle name is optional.",
                      new Point(textBox1.Top, this.textBox1.Left));
              }
          }
          

          This is exactly what I wanna do. Right now I figured out how to send the key back if shift is pressed using the sendEvent function and it works great except that function is also looking for the key as a string, which if I provide will be the altered key string not the original key, i.e. ! instead of 1.

          I am not sure how to access the key before the modifiers were pressed.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What you want is imo not possible since it depends on the keyboard layout. '1 + Shift' is not always '!'. And your .NET code also does not show that it's possible with .NET.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • L Offline
              L Offline
              lachdanan
              wrote on last edited by
              #6

              With .NET it's possible but you are thinking about it wrong.

              I am not looking to respond to !, but 1.

              So in my hotkeys if there is an action for Shift+1, it will always be 1 with Shift key held down.

              So in .NET I can just check if 1 and Shift is allocated for a valid action. I don't have to deal with the shifted characters. All I care is the unshifted key and the modifiers that act on top of the main key.

              This already works with Ctrl and Alt, but Shift changes the key name so my app can not find the right action that's on the key i.e. 1.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                @lachdanan said in Is there a way to detect pressed key combo without altering the original key without modifiers?:

                With .NET it's possible

                Please show us the .NET code.

                And since you know where the action is coming from - why do you need to know it at all?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • L Offline
                  L Offline
                  lachdanan
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher said in Is there a way to detect pressed key combo without altering the original key without modifiers?:

                  @lachdanan said in Is there a way to detect pressed key combo without altering the original key without modifiers?:

                  With .NET it's possible

                  Please show us the .NET code.

                  And since you know where the action is coming from - why do you need to know it at all?

                  It's just like in the above code:

                  You respond by the unshifted key:

                  if(e.KeyCode == Keys.D1)
                  then you perform an operation based on the active modifiers.

                  What do you mean by "And since you know where the action is coming from - why do you need to know it at all?"?

                  I have a dictionary for all primary keys without the modifiers, so if 1 is pressed, I look up the list of available actions on that key based on rules and modifiers is a way to assign more actions to a single key that way. But because 1 becomes !, I don't have any actions stored for that key.

                  In .NET, you pass the key code which will ALWAYS be 1, and from there it's up to me how to interpret the modifiers. I am not being passed ! where I need to figure out the unshifted key.

                  J.HilkJ 1 Reply Last reply
                  0
                  • L lachdanan

                    @Christian-Ehrlicher said in Is there a way to detect pressed key combo without altering the original key without modifiers?:

                    @lachdanan said in Is there a way to detect pressed key combo without altering the original key without modifiers?:

                    With .NET it's possible

                    Please show us the .NET code.

                    And since you know where the action is coming from - why do you need to know it at all?

                    It's just like in the above code:

                    You respond by the unshifted key:

                    if(e.KeyCode == Keys.D1)
                    then you perform an operation based on the active modifiers.

                    What do you mean by "And since you know where the action is coming from - why do you need to know it at all?"?

                    I have a dictionary for all primary keys without the modifiers, so if 1 is pressed, I look up the list of available actions on that key based on rules and modifiers is a way to assign more actions to a single key that way. But because 1 becomes !, I don't have any actions stored for that key.

                    In .NET, you pass the key code which will ALWAYS be 1, and from there it's up to me how to interpret the modifiers. I am not being passed ! where I need to figure out the unshifted key.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @lachdanan
                    can you show use your Qt code?

                    and is this for QWidgets or for QML ?

                    Beaus eI have the following shortcut in QML

                    Shortcut{
                            sequence: "Shift+1"
                            onActivated: {
                                console.log("Shift+1 pressed at once") 
                            }
                        }
                    

                    And it's working fine, 1 is registered as 1 and not as the shifted char, ! in my keyboard layout


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    L 1 Reply Last reply
                    3
                    • J.HilkJ J.Hilk

                      @lachdanan
                      can you show use your Qt code?

                      and is this for QWidgets or for QML ?

                      Beaus eI have the following shortcut in QML

                      Shortcut{
                              sequence: "Shift+1"
                              onActivated: {
                                  console.log("Shift+1 pressed at once") 
                              }
                          }
                      

                      And it's working fine, 1 is registered as 1 and not as the shifted char, ! in my keyboard layout

                      L Offline
                      L Offline
                      lachdanan
                      wrote on last edited by
                      #10

                      @J-Hilk

                      I am using the at interface of the app from python with pyside2. This is what my app provide wrt key and mouse actions:

                      https://www.sidefx.com/docs/houdini/hom/network.html

                      They allow you to interpret your own actions if you choose so, and that's what I am trying to do.

                      When I query the key value which is a string I get there shifted value.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        It's simply not possible with Qt since there is nothing globally defined - at first the keyboard layout is not the same everywhere and second the underlying OS may already give you the composed character.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        L 1 Reply Last reply
                        2
                        • Christian EhrlicherC Christian Ehrlicher

                          It's simply not possible with Qt since there is nothing globally defined - at first the keyboard layout is not the same everywhere and second the underlying OS may already give you the composed character.

                          L Offline
                          L Offline
                          lachdanan
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher thanks for confirming.

                          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