1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 16:29:43 +01:00

win: catch multimedia keys globally.

This commit is contained in:
Dominik Schmidt 2012-05-03 01:33:02 +02:00
parent e74ec6ba79
commit 697599db6c

View File

@ -38,6 +38,51 @@
#endif
#ifdef Q_OS_WIN
// code from patch attached to QTBUG-19064 by Honglei Zhang
LRESULT QT_WIN_CALLBACK qt_LowLevelKeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam);
HHOOK hKeyboardHook;
HINSTANCE hGuiLibInstance;
LRESULT QT_WIN_CALLBACK qt_LowLevelKeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPKBDLLHOOKSTRUCT kbHookStruct = reinterpret_cast<LPKBDLLHOOKSTRUCT>(lParam);
switch(kbHookStruct->vkCode){
case VK_VOLUME_MUTE:
case VK_VOLUME_DOWN:
case VK_VOLUME_UP:
case VK_MEDIA_NEXT_TRACK:
case VK_MEDIA_PREV_TRACK:
case VK_MEDIA_STOP:
case VK_MEDIA_PLAY_PAUSE:
case VK_LAUNCH_MEDIA_SELECT:
// send message
{
HWND hWnd = NULL;
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
// relay message to each top level widgets(window)
// if the window has focus, we don't send a duplicate message
if(QApplication::activeWindow() == widget){
continue;
}
hWnd = widget->winId();
// generate message and post it to the message queue
LPKBDLLHOOKSTRUCT pKeyboardHookStruct = reinterpret_cast<LPKBDLLHOOKSTRUCT>(lParam);
WPARAM _wParam = pKeyboardHookStruct->vkCode;
LPARAM _lParam = MAKELPARAM(pKeyboardHookStruct->scanCode, pKeyboardHookStruct->flags);
PostMessage(hWnd, wParam, _wParam, _lParam);
}
}
break;
default:
break;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
#include <io.h>
#define argc __argc
#define argv __argv
@ -46,6 +91,13 @@
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
hKeyboardHook = NULL;
hGuiLibInstance = hInstance;
// setup keyboard hook to receive multimedia key events when application is at background
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC) qt_LowLevelKeyboardHookProc, hGuiLibInstance, 0);
if (fileno (stdout) != -1 && _get_osfhandle (fileno (stdout)) != -1)
{
/* stdout is fine, presumably redirected to a file or pipe */
@ -126,7 +178,16 @@ main( int argc, char *argv[] )
a.loadUrl( arg );
}
return a.exec();
int returnCode = a.exec();
#ifdef Q_OS_WIN
// clean up keyboard hook
if( hKeyboardHook )
{
UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook = NULL;
}
#endif
return returnCode;
}