发送键“MediaPlayPause”到应用程序而不将焦点设置到它
我正在创建一个程序,它将向我拥有 IntPtr
的应用程序发送媒体键输入(例如 MediaPlayPause
、MediaNextTrack
等) 。有点像虚拟遥控器。
因此,经过研究,我发现这个, 几乎准确地告诉我如何解决我的问题。
但是,链接中提到的方法存在三个问题。
- 我无法将应用程序设置为前台窗口,因为我需要聚焦我的应用程序。
- 他们使用SendKeys函数,该函数要求目标窗口获得焦点,这违背了问题1。
- 据我所知,SendKeys无法发送键盘按钮,例如键盘播放/暂停按钮。
最后,我对必须使用什么感到相当困惑(SendInput
?,SendMessage
?)。
任何帮助将不胜感激。
EDIT
使用我收到的答案,我将下面的示例代码组合在一起。
理论上,它应该找到记事本并将字母“L”插入其中。
但是记事本上没有显示任何内容,应用程序也没有崩溃。我遗漏了一个明显的错误吗?
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(int ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, IntPtr lParam);
const int WM_KEYDOWN = 0x100;
//const int WM_KEYUP = 0x101;
const int L_KEY = 0x4C;
private void button1_Click(object sender, EventArgs e)
{
IntPtr ip = FindWindowByCaption(0, "Untitled - Notepad");
SendMessage(ip, WM_KEYDOWN, L_KEY, IntPtr.Zero);
//SendMessage(ip, WM_KEYUP, L_KEY, IntPtr.Zero);
}
I am creating a program that will send media key inputs (such as MediaPlayPause
, MediaNextTrack
, etc) to an application that I have the IntPtr
of. Sort of like a virtual remote control.
So after researching I found this, which almost tells me exactly how to solve my problem.
However, there are three problems with the approach mentioned in the link.
- I cannot set the application as foreground window as I need my application to be focused.
- They use the SendKeys function which requires the target window to be focused, goes against problem 1.
- From what I know, SendKeys cannot send keyboard buttons such as the keyboard Play/Pause button.
In the end, I am rather confused on what I have to use (SendInput
?, SendMessage
?).
Any help would be appreciated.
EDIT
Using the answer I received, I hacked together the sample code below.
Theoretically, it is supposed to find notepad and insert the letter "L" into it.
However nothing shows up on notepad, nor does the application crash. Is there an obvious error I am missing?
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(int ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, IntPtr lParam);
const int WM_KEYDOWN = 0x100;
//const int WM_KEYUP = 0x101;
const int L_KEY = 0x4C;
private void button1_Click(object sender, EventArgs e)
{
IntPtr ip = FindWindowByCaption(0, "Untitled - Notepad");
SendMessage(ip, WM_KEYDOWN, L_KEY, IntPtr.Zero);
//SendMessage(ip, WM_KEYUP, L_KEY, IntPtr.Zero);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数这些键都会转换为 WM_APPCOMMAND* 消息...因此您可以尝试
SendMessage
,另一个选项是SendInput
(如果应用程序基于 DirectInput)...查看 用于常见媒体播放器功能的 Windows API? - 也许有您可以使用一些信息...
至于焦点问题 - 没有 100% 可靠的解决方案(请参阅如何将击键发送到窗口,而无需使用 Windows API 激活它?)...您可以实现的 100% 可靠性的最佳效果是聚焦应用程序,发送键,重新聚焦您的应用程序...除非您编写某种设备驱动程序(内核模式)...
Most of these keys are translated to WM_APPCOMMAND* messages... so you can try
SendMessage
, the other option beingSendInput
(if the application is DirectInput-based)...Check out the links in Windows API for common media player functions? - perhaps there is some information you can use...
As for the focus problem - there is no 100% reliable solution (see How do I send key strokes to a window without having to activate it using Windows API?)... best you can achieve with 100% reliability is to focus the application, send the keys, refocus your application... except you would write some sort of device driver (kernel mode)...
要发送多媒体按键,包括播放/暂停、NextTrack、PrevTrack等,您可以使用
keybd_event
:这里是此 Windows api 可以处理的支持的键代码的列表:
https://learn.microsoft.com/en- us/windows/win32/inputdev/virtual-key-codes
SendKeys
类非常好,但它也有限制。上述方法将按键命令直接发送到 Windows 操作系统。To send multimedia keys, including Play/Pause, NextTrack, PrevTrack, etc, you can use
keybd_event
:Here is a list to the supported key codes that this windows api can handle:
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
The
SendKeys
class is very nice, but it's also limited. The approach above sends the key command directly to Windows OS.