Send key “MediaPlayPause” to an application without setting focus to it

依然范特西╮ 提交于 2019-12-12 20:32:26

问题


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.

  1. I cannot set the application as foreground window as I need my application to be focused.
  2. They use the SendKeys function which requires the target window to be focused, goes against problem 1.
  3. 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);
}

回答1:


Most of these keys are translated to WM_APPCOMMAND* messages... so you can try SendMessage, the other option being SendInput (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)...



来源:https://stackoverflow.com/questions/7199203/send-key-mediaplaypause-to-an-application-without-setting-focus-to-it

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