1

I am trying to create a media player controller that works while running in the background and without knowing if there is any receiver, just like pressing a key on the keyboard like the next track media key.

I have tried using keybd_event, SendInput or http://inputsimulator.codeplex.com/ but it only wsender window is in focus.

Is there any way to simulate a keyboard press like VK_MEDIA_NEXT_TRACK without having the sender window in focus?

Peter Ølsted
  • 311
  • 1
  • 4
  • 11

1 Answers1

4

This is a lot easier to implement than you might think. These keyboard keys are special, they don't produce keystrokes. The generate a WM_APPCOMMAND message instead. The command for the "Next track" key is APPCOMMAND_MEDIA_NEXTTRACK as you can tell from the MSDN library article.

That message is sent to whatever window happens to have the foreground. Which almost never does anything with it, passing it to the default window procedure instead. That's where it comes to life, it is turned into a shell command to allow a shell hook to see it. If that wasn't customized then Explorer will pick it up.

So all you have to do is send a WM_APPCOMMAND to your own window. It can be hidden, no problem. A little Winforms app will get the job done, you just need a pinvoke declaration for SendMessage(). WM_APPCOMMAND is message number 0x319. A complete example is here.

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