模拟键盘事件,如果发件人不在重点,不知道目标窗口 [英] Simulate keyboard event if sender is not in focus and without knowing target window

查看:127
本文介绍了模拟键盘事件,如果发件人不在重点,不知道目标窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个媒体播放器控制器在后台,不知道是否有任何接收器运行时,就像按下键盘像下一曲目上的一个键媒体键。

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.

我已经尝试使用 keybd_event SendInput http://inputsimulator.codeplex.com/ 但它只wsender窗口是焦点。

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

有没有什么办法来模拟键盘按下如 VK_MEDIA_NEXT_TRACK 无需对焦发送窗口?

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

推荐答案

这是一个的很多的更容易实现比你想象的。这些键盘按键比较特殊,它们不会产生击键。该生成 WM_APPCOMMAND消息来代替。对于下一曲键的命令APPCOMMAND_MEDIA_NEXTTRACK,你可以从MSDN库文章说。

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.

这消息被发送到任何窗口恰好有前景。几乎从来不与任何东西,把它传递给默认的窗口过程来代替。这就是涉及到生活中,它变成一个shell命令允许壳钩看到它。如果没有进行自定义浏览器将它捡起来。

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.

因此,所有你需要做的就是发送给WM_APPCOMMAND的您自己的窗口的。它可以被隐藏,也没有问题。一个小的WinForms应用程序将完成这项工作,你只需要一个SendMessage函数声明的PInvoke( )。 WM_APPCOMMAND是消息编号0x319。

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.

这篇关于模拟键盘事件,如果发件人不在重点,不知道目标窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆