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. Correctly detecting Ctrl + key click
Forum Updated to NodeBB v4.3 + New Features

Correctly detecting Ctrl + key click

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.6k Views 3 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.
  • J Offline
    J Offline
    JonB
    wrote on 11 Jul 2018, 11:41 last edited by
    #1

    In an event handler like QWidget::keyPressEvent() I'd like to know if user has clicked a key with a modifier (e.g. Ctrl+C for my own purposes). But look at the documentation:

    http://doc.qt.io/qt-5/qkeyevent.html#modifiers

    Returns the keyboard modifier flags that existed immediately after the event occurred.

    Warning: This function cannot always be trusted. The user can confuse it by pressing both Shift keys simultaneously and releasing one of them, for example.

    https://stackoverflow.com/a/17214947/489865 warns not to use this, and suggests:

    http://doc.qt.io/qt-5/qguiapplication.html#keyboardModifiers

    It should be noted this may not reflect the actual keys held on the input device at the time of calling but rather the modifiers as last reported in one of the above events.

    So is either one of them actually any better/more reliable than the other? Reading it seems to me that neither correctly guarantees to detect, say, Ctrl+C being pressed? So what should I do?

    1 Reply Last reply
    1
    • S SGaist
      11 Jul 2018, 21:32

      Hi,

      Depending on what you want to do wouldn't a QAction with a QShortCut fill the bill ?

      J Offline
      J Offline
      JonB
      wrote on 8 Aug 2018, 11:04 last edited by JonB
      #4

      @SGaist
      Finally got back to this. Indeed an "anonymous" action on the QTableView of QAction.setShortcut(QKeySequence.Copy) for copying the table content is a much better solution than needing to detect key presses as per the old code, so thank you.

      Note: As per https://forum.qt.io/topic/94137/qt5-qlabel-settextinteractionflags-not-behaving-as-expected/10, to restrict the shortcut to operating only on the widget and not on the window as a whole, you need to call QAction.setShortcutContext(Qt.WidgetShortcut).

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 11 Jul 2018, 21:32 last edited by
        #2

        Hi,

        Depending on what you want to do wouldn't a QAction with a QShortCut fill the bill ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 2 Replies Last reply 20 Jul 2018, 08:55
        2
        • S SGaist
          11 Jul 2018, 21:32

          Hi,

          Depending on what you want to do wouldn't a QAction with a QShortCut fill the bill ?

          J Offline
          J Offline
          JonB
          wrote on 20 Jul 2018, 08:55 last edited by
          #3

          @SGaist
          Just to say: Thanks, I have not ignored your suggestion, I just haven't had time yet to look into it. I need to recognise Ctrl+C for Copy-to-clipboard when on a selected row in a QTableView etc. (that's what the existing code allows for). If I can change it to a "higher-level" than the physical keypress using your recommendation I shall do so, and report back....

          1 Reply Last reply
          0
          • S SGaist
            11 Jul 2018, 21:32

            Hi,

            Depending on what you want to do wouldn't a QAction with a QShortCut fill the bill ?

            J Offline
            J Offline
            JonB
            wrote on 8 Aug 2018, 11:04 last edited by JonB
            #4

            @SGaist
            Finally got back to this. Indeed an "anonymous" action on the QTableView of QAction.setShortcut(QKeySequence.Copy) for copying the table content is a much better solution than needing to detect key presses as per the old code, so thank you.

            Note: As per https://forum.qt.io/topic/94137/qt5-qlabel-settextinteractionflags-not-behaving-as-expected/10, to restrict the shortcut to operating only on the widget and not on the window as a whole, you need to call QAction.setShortcutContext(Qt.WidgetShortcut).

            1 Reply Last reply
            3

            • Login

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