send Media keys

elektrobank

Member
Joined
Aug 11, 2008
Messages
19
Programming Experience
10+
I want to be able to play/pause/next/back the tracks in whatever media
player app is running. So if iTunes is running, it will receive those
commands, if Windows Media Player is, it will receive those commands,
and so on, just like my multimedia keyboard does. I can't find any way
to do this. I have tried sending:

System.Windows.Forms.Keys.MediaPlayPause,
System.Windows.Forms.Keys.MediaPreviousTrack, ect.

but it didn't do anything. Is there a way to send a key that will
control any open media player app, or do I have to have my app check
all the open apps and use the appropriate method for each app to
control it?

I'm also looking to use Keys.BrowserBack and Keys.BrowserSearch, but they don't seem to do anything either.

Any suggestions? Thanks.
 
Last edited:
SendKeys doesn't have the media keys defined, here is VB.Net translation of a found solution:
VB.NET:
Public Class Keyboard
    Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Int32, ByVal dwExtraInfo As Int32)

    Public Shared Sub SendKey(ByVal key As Keys)
        keybd_event(CByte(key), 0, 0, 0)
    End Sub
End Class
Tested this and it worked:
VB.NET:
Keyboard.SendKey(Keys.MediaPlayPause)
 
I was already using keybd_event(Keys.MediaPlayPause) and it doesn't do anything. What program are you having success with? I have tried it with iTunes and Windows media player, and neither play or pause when this is run. I was able to get vol up/down working, but the media keys don't seem to do anything.
 
I only tested with Windows Media Player 11 on Vista, didn't have to activate it or anything, I just sent the posted command from a button click. Other media applications also need to be "media keys aware", in that they do handle these keys when you actually press them on keyboard, not all applications are programmed to handle these keys.

"keybd_event(Keys.MediaPlayPause)" won't work, because keybd_event doesn't have that signature, but perhaps you didn't mean that literally?
 
I did exactly as you posted and it doesn't work. I tried it with Windows Media Player 11 on Vista as well and it didn't pause or play. Any other suggestions?
Thanks!
 
Back
Top