Results 1 to 3 of 3

Thread: Query keyboard state in Qt 3

  1. #1
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Query keyboard state in Qt 3

    At a certain point in the program I would like to find out whether or not the Control key is pressed.
    (How) can I do this in Qt3?

    So I am looking for a Qt 3 approach for Qt 4's QApplication::keyboardModifiers().

    One solution I can think of is to filter all keyboard events and to do explicit bookkeeping of whether the CTRL key is pressed, but I wonder if there is a simpler approach.

    Pieter

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Query keyboard state in Qt 3

    just example from QAsistant
    Qt Code:
    1. bool MyMainWindow::eventFilter( QObject *obj, QEvent *ev )
    2. {
    3. if ( obj == textEdit ) {
    4. if ( e->type() == QEvent::KeyPress ) {
    5. QKeyEvent *k = (QKeyEvent*)ev;
    6. qDebug( "Ate key press %d", k->key() );
    7. return TRUE;
    8. } else {
    9. return FALSE;
    10. }
    11. } else {
    12. // pass the event on to the parent class
    13. return QMainWindow::eventFilter( obj, ev );
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    read about eventFilter()
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Query keyboard state in Qt 3

    Quote Originally Posted by Pieter from Belgium
    At a certain point in the program I would like to find out whether or not the Control key is pressed.
    (How) can I do this in Qt3?

    So I am looking for a Qt 3 approach for Qt 4's QApplication::keyboardModifiers().
    I needed the same thing. Here's my implementation (I subclassed QApplication to do this).
    Qt Code:
    1. #ifdef Q_WS_WIN
    2. # include <windows.h>
    3. # include <shellapi.h>
    4. #else /* Q_WS_X11 */
    5. # include <X11/Xlib.h>
    6. #endif
    7. #include <qpaintdevice.h>
    8.  
    9. // Implement a way to easily deduce which mousebuttons and modifier
    10. // keys are held down
    11. static Qt::ButtonState get_modifiers()
    12. {
    13. int result = Qt::NoButton;
    14.  
    15. #ifdef Q_WS_WIN
    16.  
    17. result |= GetAsyncKeyState(VK_SHIFT) < 0 ? Qt::ShiftButton : 0;
    18. result |= GetAsyncKeyState(VK_MENU) < 0 ? Qt::AltButton : 0;
    19. result |= GetAsyncKeyState(VK_CONTROL) < 0 ? Qt::ControlButton : 0;
    20.  
    21. // Check if left and right mousebuttons have been swapped
    22. bool swap_buttons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
    23.  
    24. if (swap_buttons)
    25. {
    26. result |= GetAsyncKeyState(VK_LBUTTON) < 0 ? Qt::RightButton : 0;
    27. result |= GetAsyncKeyState(VK_RBUTTON) < 0 ? Qt::LeftButton : 0;
    28. }
    29. else
    30. {
    31. result |= GetAsyncKeyState(VK_LBUTTON) < 0 ? Qt::LeftButton : 0;
    32. result |= GetAsyncKeyState(VK_RBUTTON) < 0 ? Qt::RightButton : 0;
    33. }
    34. result |= GetAsyncKeyState(VK_MBUTTON) < 0 ? Qt::MidButton : 0;
    35.  
    36. #else /* Q_WS_X11 */
    37.  
    38. Window root, child;
    39. int root_x, root_y;
    40. int win_x, win_y;
    41. uint keys_buttons;
    42. bool status = XQueryPointer(QPaintDevice::x11AppDisplay(),
    43. QPaintDevice::x11AppRootWindow(),
    44. &root, &child,
    45. &root_x, &root_y,
    46. &win_x, &win_y,
    47. &keys_buttons);
    48.  
    49. if (status)
    50. {
    51. result |= keys_buttons & ShiftMask ? Qt::ShiftButton : 0;
    52. result |= keys_buttons & Mod1Mask ? Qt::AltButton : 0;
    53. result |= keys_buttons & Mod4Mask ? Qt::MetaButton : 0;
    54. result |= keys_buttons & ControlMask ? Qt::ControlButton : 0;
    55.  
    56. result |= keys_buttons & Button1Mask ? Qt::LeftButton : 0;
    57. result |= keys_buttons & Button2Mask ? Qt::MidButton : 0;
    58. result |= keys_buttons & Button3Mask ? Qt::RightButton : 0;
    59. }
    60. #endif
    61. return Qt::ButtonState(result);
    62. }
    63.  
    64. Qt::ButtonState MyApplication::keyboardModifiers()
    65. {
    66. return Qt::ButtonState(get_modifiers() & Qt::KeyButtonMask);
    67. }
    68.  
    69. Qt::ButtonState MyApplication::mouseButtons()
    70. {
    71. return Qt::ButtonState(get_modifiers() & Qt::MouseButtonMask);
    72. }
    To copy to clipboard, switch view to plain text mode 

    One solution I can think of is to filter all keyboard events and to do explicit bookkeeping of whether the CTRL key is pressed, but I wonder if there is a simpler approach.
    That's exactly how they implement it in Qt4!

Similar Threads

  1. Discussion about developing an onscreen keyboard in Qt
    By montylee in forum Qt Programming
    Replies: 6
    Last Post: 2nd January 2009, 20:09

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.