December 7, 2010

RogerCui RogerCui
Lab Rat
3 posts

How to re-direct keyboard events from Qt to Win32 windows?

 

Hi, I have a Qt window, its parent is a Win32 window.
I want to re-direct all keyboard events to the Win32 window, e.g. let it handle application shortcuts.

The code is like this,

  1. class QChild : public QWidget
  2. {
  3. pulibc:
  4.  
  5. void keyPressEvetn(QKeyEvent* e)
  6. {
  7.     int key = e->key;
  8.     // TODO: WM_KEYDOWN message needs two parameters, I'm not sure how to construct lParam from Qt.
  9.     // Are there some Qt utilities can do this?
  10. }
  11. }

2 replies

December 7, 2010

Gerolf Gerolf
Robot Herder
3287 posts

Hi,

I think, what you need is the following:

  1. class QChild : public QWidget
  2. {
  3. public:
  4.     bool winEvent(MSG* message, long* result)
  5.     {
  6.         if(WM_KEYDOWN == message->message)
  7.         {
  8.             HWND hWndParent = ::GetParent(WinId());
  9.             if(0 != hWndParent)
  10.                 *result = ::SendMessage(, message->message, message->lParam, message->wParam);
  11.         }
  12.     }
  13. }

Building the correct WPARAM for the WM_KEYDOWN event is not so easy, as it contains scan codes etc (WM_KEYDOWN on MSDN [msdn.microsoft.com]).aspx ). Make sure to also foreward the WM_KEYUP event. You could also just do a filtering before forewarding etc.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 10, 2010

RogerCui RogerCui
Lab Rat
3 posts

it works, thanks!

 
  ‹‹ Disable scrolling in QGraphicsView      Bootable Qt App using linux ››

You must log in to post a reply. Not a member yet? Register here!