mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-07 14:46:33 +02:00
win: catch multimedia keys globally.
This commit is contained in:
63
src/main.cpp
63
src/main.cpp
@@ -38,6 +38,51 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#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>
|
#include <io.h>
|
||||||
#define argc __argc
|
#define argc __argc
|
||||||
#define argv __argv
|
#define argv __argv
|
||||||
@@ -46,6 +91,13 @@
|
|||||||
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
||||||
PSTR szCmdLine, int iCmdShow)
|
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)
|
if (fileno (stdout) != -1 && _get_osfhandle (fileno (stdout)) != -1)
|
||||||
{
|
{
|
||||||
/* stdout is fine, presumably redirected to a file or pipe */
|
/* stdout is fine, presumably redirected to a file or pipe */
|
||||||
@@ -126,7 +178,16 @@ main( int argc, char *argv[] )
|
|||||||
a.loadUrl( arg );
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user