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!