2

I'm making an app that will pause/play music on an event. I am not making my own music player, rather I'd like to use the universal pause/play that windows has System wide. Is there any way to do this?

als26
  • 91
  • 9
  • The Multimdia Keys are part of the [Key enumeration](https://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx). – TaW Jan 29 '17 at 12:30
  • 1
    See if any of these post help you: [Send key “MediaPlayPause”](http://stackoverflow.com/questions/7199203/send-key-mediaplaypause-to-an-application-without-setting-focus-to-it), [Send keys through SendInput](http://stackoverflow.com/questions/12761169/send-keys-through-sendinput-in-user32-dll), [Send multimedia commands](http://stackoverflow.com/questions/15013582/send-multimedia-commands), [Codes of multimedia keys](http://stackoverflow.com/questions/8986417/codes-of-multimedia-keys) - Please report if anyone does help and maybe post the solution and an answer! – TaW Jan 29 '17 at 12:40
  • Reading through all this, it seems making your own player is simpler after all.. – TaW Jan 29 '17 at 15:53

1 Answers1

0

You'll have to use the keybd_event windows API to do this.

using System.Runtime.InteropServices;
...
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
    UIntPtr dwExtraInfo);
public const int KEYEVENTF_KEYUP = 0x0002;
public const byte VK_MEDIA_PLAY_PAUSE = 0xB3;

private void button_Click(object sender, RoutedEventArgs e)
{
    // No flags indicate key down
    keybd_event(VK_MEDIA_PLAY_PAUSE, 0, 0, UIntPtr.Zero);
    keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}

For a full list of key codes, you can use this class:

https://github.com/johnkoerner/KeyboardListener/blob/master/KeyboardListener/Keycodes.cs

TaW
  • 53,122
  • 8
  • 69
  • 111
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • Can you comment a little on this? Will it work with any application playing the e.g. music? Will it work without a MMKeyboard ? I tried and it seems to do nothing.. – TaW Jan 29 '17 at 15:48
  • Oh well, checking out Hans' post it seems this is not that easy.. – TaW Jan 29 '17 at 15:54