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. How to check the value of KeyboardModifiers?

How to check the value of KeyboardModifiers?

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

    Hi,

    I am using QMenu and inside the keyPressEvent I check the event.modifiers(), but how can I know which modifiers are active from this returned value?

    Do I have to check all combinations of ctrl, alt and shift? Though I tried this:
    (event.modifiers() == QtCore.Qt.ControlModifier | QtCore.Qt.AltModifier | QtCore.Qt.ShiftModifier)

    and it returns False, when all of these keys were active.

    I just want to check like this if possible:

    if event.modifiers().IsCtrlActive()
    append(mods, "C")
    if event.modifiers().IsShiftActive()
    append(mods, "S")
    ...

    Thanks a lot.

    jsulmJ 1 Reply Last reply
    0
    • L lachdanan

      Hi,

      I am using QMenu and inside the keyPressEvent I check the event.modifiers(), but how can I know which modifiers are active from this returned value?

      Do I have to check all combinations of ctrl, alt and shift? Though I tried this:
      (event.modifiers() == QtCore.Qt.ControlModifier | QtCore.Qt.AltModifier | QtCore.Qt.ShiftModifier)

      and it returns False, when all of these keys were active.

      I just want to check like this if possible:

      if event.modifiers().IsCtrlActive()
      append(mods, "C")
      if event.modifiers().IsShiftActive()
      append(mods, "S")
      ...

      Thanks a lot.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @lachdanan said in How to check the value of KeyboardModifiers?:

      (event.modifiers() == QtCore.Qt.ControlModifier | QtCore.Qt.AltModifier | QtCore.Qt.ShiftModifier)

      This is not how it works.
      If you want to check whether all modifiers are set:

      (event.modifiers() & QtCore.Qt.ControlModifier) && (event.modifiers() & QtCore.Qt.AltModifier) && (event.modifiers() & QtCore.Qt.ShiftModifier)
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        @jsulm said in How to check the value of KeyboardModifiers?:

        (event.modifiers() & QtCore.Qt.ControlModifier)

        Thanks so basically I can just do?:
        if (event.modifiers() & QtCore.Qt.ControlModifier):
        print "there is Ctrl in the current modifiers, not necessarily only Ctrl"

        jsulmJ 1 Reply Last reply
        0
        • L lachdanan

          @jsulm said in How to check the value of KeyboardModifiers?:

          (event.modifiers() & QtCore.Qt.ControlModifier)

          Thanks so basically I can just do?:
          if (event.modifiers() & QtCore.Qt.ControlModifier):
          print "there is Ctrl in the current modifiers, not necessarily only Ctrl"

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @lachdanan Yes, this is how bitwise operators work: https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • B Offline
            B Offline
            bluewindde
            wrote on last edited by
            #5

            Maybe this works on PySide6:

            if int(modifiers) & Qt.KeyboardModifier.ControlModifier:
                print("ctrl")
            
            Pl45m4P 1 Reply Last reply
            0
            • B bluewindde

              Maybe this works on PySide6:

              if int(modifiers) & Qt.KeyboardModifier.ControlModifier:
                  print("ctrl")
              
              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @bluewindde

              After 4 years?!?! what's the point of making the same suggestion again?


              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

              • Login

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