9

Lots of keyboards have common media functions like next/previous, play/pause and stop. Do they use some existing API in Windows, or do they implement functions specific to the most popular media players (WMP, WinAmp, Spotify...)? All keyboards I've owned which have this functionality have just seemed to work with everything, regardless of age of the keyboard vs the software, so I thought there might be an already built API for this.

If they use an already existing API in Windows, where can I find info about it?

Just to clarify: I'm not looking for a way to interact with Windows Media player specifically. I want to find that one magic button to hit to reach all (supported) media players - if one such exists.

Arve Systad
  • 5,471
  • 1
  • 32
  • 58
  • I've always wondered how it was possible to have generic remote controls for Windows, but never connected the dots - thanks for asking this question. – Mark Ransom Aug 22 '11 at 20:19

2 Answers2

7

They simply generate a virtual key code that DefWindowProc() recognizes. Copied straight from the WinUser.h header file:

#if(_WIN32_WINNT >= 0x0500)
#define VK_BROWSER_BACK        0xA6
#define VK_BROWSER_FORWARD     0xA7
#define VK_BROWSER_REFRESH     0xA8
#define VK_BROWSER_STOP        0xA9
#define VK_BROWSER_SEARCH      0xAA
#define VK_BROWSER_FAVORITES   0xAB
#define VK_BROWSER_HOME        0xAC

#define VK_VOLUME_MUTE         0xAD
#define VK_VOLUME_DOWN         0xAE
#define VK_VOLUME_UP           0xAF
#define VK_MEDIA_NEXT_TRACK    0xB0
#define VK_MEDIA_PREV_TRACK    0xB1
#define VK_MEDIA_STOP          0xB2
#define VK_MEDIA_PLAY_PAUSE    0xB3
#define VK_LAUNCH_MAIL         0xB4
#define VK_LAUNCH_MEDIA_SELECT 0xB5
#define VK_LAUNCH_APP1         0xB6
#define VK_LAUNCH_APP2         0xB7

#endif /* _WIN32_WINNT >= 0x0500 */

Since all windows call DefWindowProc(), you can simply use SendInput or keybd_event to send the keystroke.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
6

this is just a virtual key code - for an official list see MSDN.

There you find for example VK_VOLUME_UP VK_MEDIA_PLAY_PAUSE VK_ZOOM

Even some Remotes translate to these codes to be as compatible as possible with existing software..

EDIT - as per comment:

These were introduced back in the day when Windows ME (!) came out and are still in use - at least when I checked the registry of my Windows 2008 R2 !

Basically Windows translates certain VK* into WM_APPCOMMAND messages with certain codes which the applications listen to...
If the key has something to do with launching an app to do (i.e. Mail, Browser etc.) then the magic happens via Windows Explorer which reads the mapping (either by association or direct exec) from the registry at Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ AppKey - either HKLM or HKCU.

Some links with old but as it seems still valid information:

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • ...but the underlying functionality in Windows that use these key codes is not exposed anywhere? – Arve Systad Aug 22 '11 at 20:02
  • you mean how windows knows for example which application to start when someone presse `VK_LAUNCH_MAIL` ? – Yahia Aug 22 '11 at 20:09
  • 1
    Not necessarily how it knows, but whatever function/method it calls down below. I mean, since Windows recognizes so many different apps that can use e.g. play/pause functionality, it must have some sort of common interface to look for, that can be triggered through a specific function in Windows itself...? – Arve Systad Aug 22 '11 at 20:13
  • @Arve Systad, there is no functionality in Windows, each media application is expected to respond to the keys with the appropriate action for that app. – Mark Ransom Aug 22 '11 at 20:15
  • 1
    @Arve Systad, if you're asking about the Windows API used to play a media file that's a completely different question. The API does not respond directly to the keys, the app responds to the keys and relays the command to the API. – Mark Ransom Aug 22 '11 at 20:17
  • 1
    @Mark Ransom: Ah, that makes sense. App listens to keys. I thought of the whole thing completely the other way around. That clarifies it all. I'd accept your answer if it was a separate post :-) – Arve Systad Aug 22 '11 at 20:20
  • 1
    as it seems you both are right - see my edit for how the magic happens :-) – Yahia Aug 22 '11 at 20:22
  • 2
    Thanks for the latest edit. I was assuming that the app responds directly to the keys, but that's not correct; your links explain that Windows translates the keys automatically into WM_APPCOMMAND messages. Wish I could give another +1. – Mark Ransom Aug 22 '11 at 20:47