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,
@
class QChild : public QWidget
{
pulibc:void keyPressEvetn(QKeyEvent* e)
{
int key = e->key;
// TODO: WM_KEYDOWN message needs two parameters, I'm not sure how to construct lParam from Qt.
// Are there some Qt utilities can do this?
}
}
@ -
Hi,
I think, what you need is the following:
@
class QChild : public QWidget
{
public:
bool winEvent(MSG* message, long* result)
{
if(WM_KEYDOWN == message->message)
{
HWND hWndParent = ::GetParent(WinId());
if(0 != hWndParent)
*result = ::SendMessage(, message->message, message->lParam, message->wParam);
}
}
}
@Building the correct WPARAM for the WM_KEYDOWN event is not so easy, as it contains scan codes etc ("WM_KEYDOWN on MSDN":http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx ). Make sure to also foreward the WM_KEYUP event. You could also just do a filtering before forewarding etc.
-
it works, thanks!