Capture Media Keys when Application is Minimized

て烟熏妆下的殇ゞ 提交于 2019-11-27 07:43:42

问题


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?


回答1:


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.




回答2:


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.




回答3:


Use global keyboard hooks (Processing Global Mouse and Keyboard Hooks in C#)

Just create the hook when starting your app/form. You can set KeyEventHandlers for the global key up, down and press events. You can then check to see if it is the Key combination you are looking for.




回答4:


For this prob I implemented this solution:

http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx



来源:https://stackoverflow.com/questions/7196883/capture-media-keys-when-application-is-minimized

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!