Media Keyboard Buttons

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
How can I attach events to media keys which are on keyboards?

Thanks.
 
There are media keys defined in the Keys enumeration like any other regular key.
 
Oh ok thanks.

How do i make it so that the program does not have to be in focus for the action to occur?

Thanks.

EDIT: I cant seem to get any sort of response from my media keys on my LX700 Logitech keyboard, but i can from standard keys.
 
Last edited:
Actually they don't here either when I got to try, on MS MultiMedia KB. The keys are functional in Windows but don't tell in VB events. Just about nothing on the web about these keys to find.

Global system keyboard listener? Not integrated .Net functionality, you need either a keyboard hook or implement the Win32 hotkey functions.
 
Global system keyboard listener? Not integrated .Net functionality, you need either a keyboard hook or implement the Win32 hotkey functions.

Thanks. Adapter some C# code i found via Google.

Still looking for the media keys solution tho.
 
Finally found it! (admittedly I haven't looked into this since last time :p ) These buttons are defined as system keys that don't produce keyboard event for the controls keydown/keypress/keyup events. To get these messages you have to watch some kind of pre-filter of messages, in code this is how:
VB.NET:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    Select Case keyData
        Case Keys.MediaNextTrack
        Case Keys.MediaPreviousTrack
        Case Keys.MediaPlayPause
            MsgBox("Media Play button detected!")
        Case Keys.MediaStop
    End Select
    Return MyBase.ProcessDialogKey(keyData)
End Function
You could also have a read at Probably more than you want to know about keyboarding in Windows Forms….
 
Thanks for the reply!

Maybe its just me, but what you provided doesn't seem to work straight out of the box....I am guessing i have to do some reading and change it?

I have a Logitech LX700 Keyboard (I think).

EDIT:

It doesn't trigger anything on my keyboard for the media keys, but is triggered by Esc.

EDIT again:

Rather funny, my special hotkeys that open certain folders like "my music", "my videos, "my pictures" and "my documents" at the top of the keyboard trigger the event.

Edit again:

Infact is seems to be only the media keys that don't trigger this event.
 
If the media keys don't work for you with this code, it must be the keyboards fault. It works with my MS keyboard. I have experienced a Logitech keyboard some years ago that didn't work for such special keys in Windows unless there was some special driver software installed, I guess they have assigned their key codes and translate this with their own software so that the operating system get them, or do simple signal-to-process-start mapping with the software. I was more hoping to hear that this was a ghost of the past because of non-standarized special/media key codes in the early days of "extended keys", and that this was sorted out between the manufacturors long time ago, but it seems they haven't yet. You could try to find a general keyboard hook and detect what/if happens for these keys, but chances are they are not interpreted as keys by the operating system at all.
 
Back
Top