I don't have the time to look into the new API-version or use a keyboard that sends appcommand-messages so I cannot update it for now, however here is the code if anyone is interested in it. Feel free to do anything with it!
Defines:
#define HSHELL_APPCOMMAND 12
#define FAPPCOMMAND_MASK 0xF000
#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK))
#define APPCOMMAND_MEDIA_NEXTTRACK 11
#define APPCOMMAND_MEDIA_PREVIOUSTRACK 12
#define APPCOMMAND_MEDIA_STOP 13
#define APPCOMMAND_MEDIA_PLAY_PAUSE 14
#define WM_APPCOMMAND 0x0319
The init function:
// Add the ShellProc hook
hShellHook = SetWindowsHookEx(
WH_SHELL, // Hook in before msg reaches app
(HOOKPROC) ShellProc, // Hook procedure
core_api::get_my_instance(), // This DLL instance
0L // Hook in to all apps
);
The quit function:
UnhookWindowsHookEx(hShellHook);
The main function:
// Hook procedure for Shell hook
LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// Do we have to handle this message?
if (nCode == HSHELL_APPCOMMAND)
{
short AppCommand = GET_APPCOMMAND_LPARAM(lParam);
switch (AppCommand)
{
case APPCOMMAND_MEDIA_NEXTTRACK:
keybd_event(VK_MEDIA_NEXT_TRACK,0,0,0);
return 1;
case APPCOMMAND_MEDIA_PLAY_PAUSE:
keybd_event(VK_MEDIA_PLAY_PAUSE,0,0,0);
return 1;
case APPCOMMAND_MEDIA_PREVIOUSTRACK:
keybd_event(VK_MEDIA_PREV_TRACK,0,0,0);
return 1;
case APPCOMMAND_MEDIA_STOP:
keybd_event(VK_MEDIA_STOP,0,0,0);
return 1; // dont call CallNextHookEx, instead return non-zero, because we have handled the message (see MSDN doc)
}
}
// Call the next handler in the chain
return CallNextHookEx (hShellHook, nCode, wParam, lParam);
}