9

After a lot of research on Stackoverflow and google, it seems that it's difficult to send a combination of keystroke to a background window using it's handle. For example, I want to send CTRL + F. It seems that Sendmessage doesn't work, and sendinput isn't effective because the window needs the focus.

So the my last thought is about hooking: is there anyway to use that way to send combination?

4

2 回答 2

8

好的,我找到了一种解决方法,但它不适用于所有应用程序。否则,它可以与我想用按键组合控制的程序 puTTY 一​​起使用。即使应用程序没有重点,它也可以工作。所以我现在完成了!

class SendMessage
{
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

public static void sendKeystroke()
{
    const uint WM_KEYDOWN = 0x100;
    const uint WM_KEYUP = 0x0101;

    IntPtr hWnd;
    string processName = "putty";
    Process[] processList = Process.GetProcesses();

    foreach (Process P in processList)
    {
        if (P.ProcessName.Equals(processName))
        {
            IntPtr edit = P.MainWindowHandle;
            PostMessage(edit, WM_KEYDOWN, (IntPtr)(Keys.Control), IntPtr.Zero);
            PostMessage(edit, WM_KEYDOWN, (IntPtr)(Keys.A), IntPtr.Zero);
            PostMessage(edit, WM_KEYUP, (IntPtr)(Keys.Control), IntPtr.Zero);
        }
    }                           
}

}
于 2012-10-15T20:43:22.803 回答
5

我编写了几个将击键发送到后台窗口的程序,我通常实现 PostMessage/SendMessage。我在这里记录了我的所有发现!

但是您基本上将使用低级别的 c 调用将消息放入 Windows 消息队列,以允许应用程序获取按键。

留言

发信息

如果您有任何问题,请告诉我,我的库是用 C# 编写的,我很乐意分享。此方法还允许在背景窗口中使用鼠标:)

所有代码都签入 GitHub:https ://github.com/EasyAsABC123/Keyboard

于 2013-01-22T18:48:11.163 回答