Results 1 to 5 of 5

Thread: Detect If Control (Ctrl) ANd Another Key Is Pressed KEY_F

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Detect If Control (Ctrl) ANd Another Key Is Pressed KEY_F

    Good DAy im trying to catch CTRL + F Key in my code and i can cath if f is pressed by doing the following

    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *e)
    2. {
    3. if ( (e->key() == Qt::Key_F) ) //&& ( e->state() & ControlButton )
    4. {
    5. qDebug()<<" Correct Key";
    6. }
    7.  
    8. else
    9. {
    10. qDebug()<<"Not Correct Key";
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    However I need to kknow if ctrl is pressed as well. Could someone please help me with this regard

  2. The following user says thank you to ShapeShiftme for this useful post:

    Thành Viên Má»›i (28th May 2011)

  3. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detect If Control (Ctrl) ANd Another Key Is Pressed KEY_F

    try

    Qt Code:
    1. QApplication::keyboardModifiers() & Qt::ControlModifier
    To copy to clipboard, switch view to plain text mode 

  4. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Detect If Control (Ctrl) ANd Another Key Is Pressed KEY_F

    just check documentation of QKeyEvent.

  5. #4
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Detect If Control (Ctrl) ANd Another Key Is Pressed KEY_F

    Quote Originally Posted by FelixB View Post
    try

    Qt Code:
    1. QApplication::keyboardModifiers() & Qt::ControlModifier
    To copy to clipboard, switch view to plain text mode 
    Thank you that worked great and for anyone that wants to know where to insert the code do the following

    Qt Code:
    1. void frm_web::keyPressEvent(QKeyEvent *e)
    2. {
    3. if ( (e->key() == Qt::Key_F) && QApplication::keyboardModifiers() && Qt::ControlModifier)
    4. {
    5. qDebug()<<" Correct Key";
    6. //connect my signal and slot
    7. }
    8.  
    9. else
    10. {
    11. qDebug()<<"Not Correct Key";
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thanks Again

  6. The following user says thank you to ShapeShiftme for this useful post:

    Pluvius (21st December 2012)

  7. #5
    Join Date
    Mar 2008
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect If Control (Ctrl) ANd Another Key Is Pressed KEY_F

    Your line number 3) has a bug. You Control key check must be a field manipulation operator(&) and not (&&). The correct line of code would be:

    Qt Code:
    1. if ((e->key() == Qt::Key_F) && (QApplication::keyboardModifiers() & Qt::ControlModifier))
    To copy to clipboard, switch view to plain text mode 

    Another way to do the same for people who are not that familiar with field manipulation functions is the following line identical to the line above:

    Qt Code:
    1. if ((e->key() == Qt::Key_F) && (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier)))
    To copy to clipboard, switch view to plain text mode 

    I would also recommend using the modifiers coming from the key event itself and not the global app keyboard modifiers. i.e. the best way to write what you need is:

    Qt Code:
    1. if ((e->key() == Qt::Key_F) && (e->modifiers().testFlag(Qt::ControlModifier)))
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to tiho_d for this useful post:

    sami1592 (26th February 2016)

Similar Threads

  1. How to detect hover events when mouse button is pressed
    By yagabey in forum Qt Programming
    Replies: 12
    Last Post: 26th April 2016, 09:23
  2. Replies: 6
    Last Post: 4th October 2010, 03:19
  3. Replies: 0
    Last Post: 26th June 2010, 00:40
  4. Replies: 0
    Last Post: 16th December 2009, 09:45
  5. Replies: 2
    Last Post: 3rd July 2009, 21:05

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.