Simulate keyboard event if sender is not in focus and without knowing target window [duplicate]

廉价感情. 提交于 2019-12-02 15:08:34

问题


I am trying to create a media player controller that works while running in the background and without knowing if there is any receiver, just like pressing a key on the keyboard like the next track media key.

I have tried using keybd_event, SendInput or http://inputsimulator.codeplex.com/ but it only wsender window is in focus.

Is there any way to simulate a keyboard press like VK_MEDIA_NEXT_TRACK without having the sender window in focus?


回答1:


This is a lot easier to implement than you might think. These keyboard keys are special, they don't produce keystrokes. The generate a WM_APPCOMMAND message instead. The command for the "Next track" key is APPCOMMAND_MEDIA_NEXTTRACK as you can tell from the MSDN library article.

That message is sent to whatever window happens to have the foreground. Which almost never does anything with it, passing it to the default window procedure instead. That's where it comes to life, it is turned into a shell command to allow a shell hook to see it. If that wasn't customized then Explorer will pick it up.

So all you have to do is send a WM_APPCOMMAND to your own window. It can be hidden, no problem. A little Winforms app will get the job done, you just need a pinvoke declaration for SendMessage(). WM_APPCOMMAND is message number 0x319.



来源:https://stackoverflow.com/questions/16743181/simulate-keyboard-event-if-sender-is-not-in-focus-and-without-knowing-target-win

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