Results 1 to 7 of 7

Thread: how to catch key combination inside keyPressEvent.

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default how to catch key combination inside keyPressEvent.

    hi
    i want to catch a combination of keypresses inside keyPressEvent such as shift+K;
    normally single key presses can be caught inside keyPressEvent using case statements .

    how to catch combination of keypresses inside keyPressEvent.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to catch key combination inside keyPressEvent.

    See QKeyEvent docs.

    Hint: Shift is so called modifier.
    J-P Nurmi

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: how to catch key combination inside keyPressEvent.

    i tried like the below and it did not worked. how to use the modifier inside keypressevent.

    Qt Code:
    1. void GLWidget::keyPressEvent(QKeyEvent * event) {
    2. switch (event->key()) {
    3. case (Qt::Key_1):
    4. if (event->modifiers()==Qt::ShiftModifier)
    5. qDebug()<<"shift and one";
    6. break;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to catch key combination inside keyPressEvent.

    That's the way keyboards work. When you press Shift+1, at least on western keyboards it produces an exclamation mark ("!"), not one ("1") right? There is Qt::Key_Exclam for that. You might be able to get Shift+1 when you use numpad.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: how to catch key combination inside keyPressEvent.

    i want to enter in to a case(switch-case) when shift and one (Shift+1) is pressed .how do i do it inside keyPressEvent.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to catch key combination inside keyPressEvent.

    As I said, you can do it with the numpad only. And you will have to change the if-statement because Qt::KeypadModifier will be active as well:
    Qt Code:
    1. if (event->modifiers() & Qt::ShiftModifier)
    2. ...
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following 2 users say thank you to jpn for this useful post:

    babu198649 (13th May 2008), spagatoni (14th July 2009)

  8. #7
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: how to catch key combination inside keyPressEvent.

    thanks jpn it works,
    is there any advantage in using bitwise AND operator in preference to == operator(although both works). How does the bitwise AND operator works here(ie. logic ) .

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.