Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture Media Keys when Application is Minimized

Tags:

c#

keyboard

media

I want to provide an option with a media player I'm working on for the media keys to work even when it's minimized. What's the best way to capture and process those key events in C# without having focus? Is it even possible?

like image 860
Adam Haile Avatar asked Aug 25 '11 20:08

Adam Haile


2 Answers

I don't think that there is an easy way to do this but this (Using Window Messages to Implement Global System Hooks in C#) project may help. I added below code

    _GlobalHooks.Keyboard.KeyboardEvent += (w, l) => 
    {
          this.LblMouse.Text = "KEY: " + w.ToInt32().ToString("X8") + " " + l.ToInt32().ToString("X8");
    };
    _GlobalHooks.Keyboard.Start();

to the constructor of Form1 of GlobalHookTest and was able to monitor all the keyboard events.

like image 186
L.B Avatar answered Nov 06 '22 05:11

L.B


You can do that but only with some global hooking - for source code and details see

  • http://globalmousekeyhook.codeplex.com/
  • http://www.codeproject.com/KB/system/globalsystemhook.aspx (more low level and will get any keys)

EDIT:

BEWARE that some Media Keys get translated into APP_COMMAND Windows messages - so you should think about hooking those too.

IF you want to make your mediaplayer automagically start on the press of a (media) key see the links here.

like image 3
Yahia Avatar answered Nov 06 '22 05:11

Yahia