Results 1 to 9 of 9

Thread: How to catch "ENTER" with keyPress?

  1. #1
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    1
    Thanked 4 Times in 1 Post

    Default How to catch "ENTER" with keyPress?

    Im writing a little game in opengl game using QT(in c++). I got a fullscreen widget and have been using keyPress for keys like down,left,up,down. But when I tried with Enter and Space it don't work. Actually without doing anything, pressing "Enter" messes up animations and stuff. I read some in the documentation and it says it has something to do with focusing maybe? Like with tab, do I have to reimplement QEvent? How? =]

    Im using QT 3.2.2 and its my first experience with QT.
    Last edited by lumber44; 14th February 2006 at 07:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to catch "ENTER" with keyPress?

    Can you show your code that is catching the "enter" key?
    Basically you need to overload keyPressEvent(), and check for the key code Qt::Key.
    Should not be a problem.

  3. #3
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    1
    Thanked 4 Times in 1 Post

    Default Re: How to catch "ENTER" with keyPress?

    This is what I have, I only have problem with ENTER and SPACE of the keys I have tried.

    Qt Code:
    1. void GM_QtWindow::keyPressEvent( QKeyEvent *k )
    2. {
    3. paintGL();
    4. unsigned char key = k->ascii();
    5.  
    6. if(key !=0)
    7. {
    8. _keyboard(key,0,0);
    9. }
    10. else
    11. {
    12. int speckey = k->key();
    13.  
    14. switch(speckey)
    15. {
    16. case Key_A:
    17. _keyboard('a',0,0);
    18. break;
    19. case Key_Up:
    20. _specKeyboard(GM_KEY_UP,0,0);
    21. break;
    22. case Key_Down:
    23. _specKeyboard(GM_KEY_DOWN,0,0);
    24. break;
    25. case Key_Left:
    26. _specKeyboard(GM_KEY_LEFT,0,0);
    27. break;
    28. case Key_Right:
    29. _specKeyboard(GM_KEY_RIGHT,0,0);
    30. break;
    31. case Key_PageUp:
    32. _specKeyboard(GM_KEY_PAGE_UP,0,0);
    33. break;
    34. case Key_PageDown:
    35. _specKeyboard(GM_KEY_PAGE_DOWN,0,0);
    36. break;
    37. case Key_Home:
    38. _specKeyboard(GM_KEY_HOME,0,0);
    39. break;
    40. case Key_End:
    41. _specKeyboard(GM_KEY_END,0,0);
    42. break;
    43. case Key_Insert:
    44. _specKeyboard(GM_KEY_INSERT,0,0);
    45. break;
    46. //not working
    47. case Key_Enter:
    48. cout << "PRESSING ENTER\n";
    49. _specKeyboard(GM_KEY_ENTER,0,0);
    50. break;
    51. case Key_Space:
    52. _specKeyboard(GM_KEY_SPACE,0,0);
    53. break;
    54.  
    55. //mod-keys
    56. case Key_Shift:
    57. _setModKey(GM_SHIFT_BUTTON); _message("Shift-button down");
    58. break;
    59. case Key_Control:
    60. _setModKey(GM_CONTROL_BUTTON); _message("Ctrl-button down");
    61. break;
    62. case Key_Alt:
    63. _setModKey(GM_ALT_BUTTON); _message("Alt-button down");
    64. break;
    65.  
    66. default:
    67. break;
    68. }//end switch
    69. }
    70. paintGL();
    71. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to catch "ENTER" with keyPress?

    add Key_unknown to the defalut case and see if it gets cought.
    You can also put a qDebug("cought key:%d",k->key()) to see what key code is being cought and try to find it in the docs.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to catch "ENTER" with keyPress?

    Remember that the so called "ENTER" key, is really called "Return". "Enter" is the key on the numeric keypad. The big one above shift is called "Return" and is handled by Key_Return.

  6. #6
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to catch "ENTER" with keyPress?

    Quote Originally Posted by lumber44
    This is what I have, I only have problem with ENTER and SPACE of the keys I have tried.

    void GM_QtWindow::keyPressEvent( QKeyEvent *k )
    {
    paintGL();
    unsigned char key = k->ascii();

    if(key !=0)
    {
    _keyboard(key,0,0);
    }
    else
    {
    int speckey = k->key();
    ....
    you could also do

    if(key==13) //13 being the ascii code for enter
    {

    //do action for the enter key
    }

  7. #7
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    1
    Thanked 4 Times in 1 Post

    Default Re: How to catch "ENTER" with keyPress?

    The Key_Return didn't work but the catching of ascii code 13 works. I can now catch Enter but still, it isnt good. Cause every time I hit Enter,space or Tab(if I catch it or not) the widget "freezes", I think it gets/lose focus or something. These keys are handled in some manner in QEvent?("freezes" = all timers stop and stuff )

    From the qt docs (Keyboard Focus):

    Another exception to Tab support is text-entry widgets that must support the insertion of tabs; almost all text editors fall into this class. Qt treats Control+Tab as Tab and Control+Shift+Tab as Shift+Tab, and such widgets can reimplement QWidget::event() and handle Tab before calling QWidget::event() to get normal processing of all other keys. However, since some systems use Control+Tab for other purposes, and many users aren't aware of Control+Tab anyway, this isn't a complete solution.
    This may be my problem? or am I doing something wrong? =)

  8. #8
    Join Date
    Feb 2006
    Posts
    6
    Thanks
    1
    Thanked 4 Times in 1 Post

    Default Re: How to catch "ENTER" with keyPress?

    Hehe, well after reading the docs, I just did what it said I thought it would be alot of work, but it turned out to be quite easy . Its all working now, this is my solution if your interested. Thanks for all your help

    Qt Code:
    1. bool GM_QtWindow::event(QEvent* qe)
    2. {
    3. if (qe->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent *ke = (QKeyEvent *) qe;
    6. if (ke->key() == Key_Tab)
    7. {
    8. cout << "LOL";
    9. return true;
    10. }
    11. if (ke->key() == Key_Enter)
    12. {
    13. _specKeyboard(GM_KEY_ENTER,0,0);
    14. return true;
    15. }
    16. if (ke->key() == Key_Return)
    17. {
    18. _specKeyboard(GM_KEY_ENTER,0,0);
    19. return true;
    20. }
    21. if (ke->key() == Key_Space)
    22. {
    23. _specKeyboard(GM_KEY_SPACE,0,0);
    24. return true;
    25. }
    26. }
    27. return QWidget::event(qe);
    28. }
    To copy to clipboard, switch view to plain text mode 

  9. The following 4 users say thank you to lumber44 for this useful post:

    ePharaoh (21st July 2006), forrestfsu (21st February 2007), pavanbarot (23rd December 2010), Siena (5th April 2013)

  10. #9
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to catch "ENTER" with keyPress?

    Has anyone considered using the QShortcut class to handle intercepting keyboard shortcuts? I had a table widget that was "stealing" the enter and tab keys from me and I got around the problem by using QShortCut as follows:

    //Intercept the CTRL-TAB key from the table widget
    QShortcut* shortcutTabKey;
    shortcutTabKey = new QShortcut( QKeySequence( tr("Ctrl+TAB") ), m_tableWidget );
    connect( shortcutTabKey, SIGNAL( activated() ), this, SLOT( OnTableShortCutTabKey() ) );

    Hopefully this helps someone, as it seems easier than overriding QWidget::event.

  11. The following user says thank you to joshlareau for this useful post:

    Siena (5th April 2013)

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.