Results 1 to 8 of 8

Thread: eventFilter: catch both Control and Shift modifiers

  1. #1

    Default eventFilter: catch both Control and Shift modifiers

    Hello,

    I am overriding an eventFilter, and I am trying to catch both Shift and Control modifiers.
    I am unfortunately unable to get the work together.

    I tried both

    Qt Code:
    1. QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
    2. if (key_event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier))
    3. {
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. Qt::KeyboardModifiers keyMod = QApplication::keyboardModifiers ();
    2. bool isSHIFT = keyMod.testFlag(Qt::ShiftModifier);
    3. bool isCTRL = keyMod.testFlag(Qt::ControlModifier);
    4.  
    5. if (isSHIFT && isCTRL)
    6. {
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 

    Does someone know how to do it?

    Luca

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter: catch both Control and Shift modifiers

    Your first snippet looks ok to me, but maybe try to check each separately?

    Qt Code:
    1. if ((event->modifiers() & Qt::ShiftModifier) != 0 && (event->modifiers() & Qt::ControlModifier) != 0)
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3

    Default Re: eventFilter: catch both Control and Shift modifiers

    Hi anda_skoa,

    thanks for your help. Actually it doesn't work.

    With this:

    Qt Code:
    1. else if (key_event->key() == Qt::Key_U)
    2. if (key_event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier))
    To copy to clipboard, switch view to plain text mode 

    it seems that Shift+U and Ctrl+U work separately.
    With your solution instead:

    Qt Code:
    1. if ((key_event->modifiers() & Qt::ShiftModifier) != 0 && (key_event->modifiers() & Qt::ControlModifier) != 0)
    To copy to clipboard, switch view to plain text mode 

    neither separate shortcuts work, nor together.

    The fact is that I am trying to override the default Ctrl+Shift+U unicode Linux's shortcut. Is it possible?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: eventFilter: catch both Control and Shift modifiers

    The fact is that I am trying to override the default Ctrl+Shift+U unicode Linux's shortcut.
    So does this code work (or not) for -other- non-system Ctrl-Shift-something combinations? Or just not work at all for anything?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter: catch both Control and Shift modifiers

    Quote Originally Posted by LucaDanieli View Post
    With your solution instead:

    Qt Code:
    1. if ((key_event->modifiers() & Qt::ShiftModifier) != 0 && (key_event->modifiers() & Qt::ControlModifier) != 0)
    To copy to clipboard, switch view to plain text mode 

    neither separate shortcuts work, nor together.
    Maybe your event filter returns true and filters the event before it gets to the actual shortcut handler?

    The code above is just a check for both modifiers being present, it doesn't do anything.

    Cheers,
    _

  6. #6

    Default Re: eventFilter: catch both Control and Shift modifiers

    Difficult to say.
    I would say that it doesn't work at all.

    I try with Qt::Key_9

    Qt Code:
    1. else if (key_event->key() == Qt::Key_9)
    2. if (key_event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier))
    3. {
    4. lookupReferences();
    5. event->ignore();
    6. return true;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Shift+9 = ")" [Italian Keyboard]
    Ctrl+9 = works
    Ctrl+Shift+9 = ")"

    From this, I would say that "|" means "or".

    Qt Code:
    1. if ((key_event->modifiers() & Qt::ShiftModifier) != 0 && (key_event->modifiers() & Qt::ControlModifier) != 0)
    To copy to clipboard, switch view to plain text mode 

    Ctrl+Shift+9 = ")"
    Ctrl+9 = nothing
    Shift+9 = ")"

    but

    Qt Code:
    1. else if (key_event->key() == Qt::Key_U)
    2. if (key_event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier))
    To copy to clipboard, switch view to plain text mode 

    Shift+U = works
    Ctrl+U = works


    There is something weird here.
    Later I will try more combinations

  7. #7

    Default Re: eventFilter: catch both Control and Shift modifiers

    Yes, I checked again.
    It works exactly as reported above.

    I really don't understand this behaviour. "Shift+9" and "Shift+U" respond differently, and generally I can't trig Ctrl+Shift+Key, but only Ctrl+Key or Shift+Key.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: eventFilter: catch both Control and Shift modifiers

    I assume you have checked that without any specific key?
    E.g. just checked for both modifiers and then print the key whatever it is?

    Did you install the event filter on a specific widget or on the application object?

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 22nd September 2015, 12:26
  2. how to catch event for LineEdit control
    By gauravg in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2011, 20:08
  3. Get pressed keys (no modifiers) from QMouseEvent
    By mariposa in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2010, 11:44
  4. how to catch Shift + Left arrow? [Soved]
    By thomaspu in forum Qt Programming
    Replies: 2
    Last Post: 28th December 2007, 23:16
  5. Access Specifiers, Modifiers and Qualifiers
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 8th March 2006, 12:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.