【问题标题】:Send keystroke to application in c# (sendkeys, postmessage, sendmessage all not working)在 C# 中向应用程序发送击键(sendkeys、postmessage、sendmessage 都不起作用)
【发布时间】:2013-03-20 23:50:38
【问题描述】:

我正在尝试执行以下操作之一 1.打开所需程序并以编程方式按键 2.找到程序的打开窗口并以编程方式按键 (任何一个都可以)

我尝试了许多 SendKeys.SendWait()、PostMessage() 和 SendMessage() 的实现,但均未成功。下面是我的代码sn-ps

    //included all these for attempts 
    [DllImport("User32.dll")] 
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  

    [DllImport("User32.dll")] 
    static extern int SetForegroundWindow(IntPtr hWnd);

    [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
    static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

    [DllImport("user32.dll")]
    static extern byte VkKeyScan(char ch);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

获取窗口句柄,sendmessage/postmessage/sendkeys使用的变量

IntPtr ptrOBS = proc.Handle;//this works properly, proc is instantiated properly
//IntPtr ptrOBS = FindWindow(null, "Open Broadcaster Software v0.472b");
SetForegroundWindow(ptrOBS);
const UInt32 WM_CHAR = 0x0102;
const uint WM_KEYDOWN = 0x100;
const int VK_R = 0x52; // taken from http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
const int VK_S = 0x53;

发送消息尝试:

SendMessage(ptrOBS, WM_KEYDOWN, (IntPtr)VK_R, (IntPtr)1);//tried both WM_CHAR and WM_KEYDOWN

PostMessage 尝试:

string message = "rs";
bool sent = PostMessage(ptrOBS, WM_KEYDOWN, VkKeyScan(message[0]), 0);

发送密钥尝试:

SendKeys.SendWait("{r}");

在父窗口(应用程序)和子窗口(按键触发的按钮我试图发送)上尝试了 SetFocus:

static void SetFocus(IntPtr hwndTarget, string childClassName)
    {
        // hwndTarget is the other app's main window 
        // ...
        IntPtr targetThreadID = GetWindowThreadProcessId(hwndTarget, IntPtr.Zero); //target thread id
        IntPtr myThreadID = GetCurrentThread(); // calling thread id, our thread id
        try
        {
            bool lRet = AttachThreadInput(myThreadID, targetThreadID, -1); // attach current thread id to target window

            // if it's not already in the foreground...
            lRet = BringWindowToTop(hwndTarget);
            SetForegroundWindow(hwndTarget);

            // if you know the child win class name do something like this (enumerate windows using Win API again)...
            IntPtr hwndChild = (IntPtr)1183492;//(IntPtr)EnumAllWindows(hwndTarget, childClassName).FirstOrDefault();

            if (hwndChild == IntPtr.Zero)
            {
                // or use keyboard etc. to focus, i.e. send keys/input...
                // SendInput (...);
                return;
            }

            // you can use also the edit control's hwnd or some child window (of target) here
            SetFocus(hwndChild); // hwndTarget);
            SendKeys.SendWait("{r}");
        }
        finally
        {
            SendKeys.SendWait("{r}");
            bool lRet = AttachThreadInput(myThreadID, targetThreadID, 0); //detach from foreground window
            SendKeys.SendWait("{r}");
        }
    }

对于 NSGaga:

    string windowName = "Open Broadcaster Software v0.472b";
IntPtr outerPtr = FindWindow(null, windowName);
IntPtr ptrOBS = (IntPtr)527814;//button that im trying to trigger keypress on
SetForegroundWindow(outerPtr);
SetForegroundWindow(ptrOBS);
SetFocus(outerPtr, "OBSWindowClass");//SetFocus(ptrOBS, "Button");
const UInt32 WM_CHAR = 0x0102;
const int VK_R = 0x52; // taken from http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
const int VK_S = 0x53;

//SetForegroundWindow(ptrOBS);
System.Threading.Thread.Sleep(3000);
SendKeys.SendWait("{r}");
SendMessage(outerPtr, WM_KEYDOWN, (IntPtr)VK_R, (IntPtr)1);
PostMessage(outerPtr, WM_KEYDOWN, VkKeyScan('r'), 0);

【问题讨论】:

标签: c# .net windows keyboard-events sendkeys


【解决方案1】:

You cannot reliably use SendMessage and PostMessage for synthesizing keyboard input。它们只是不是为此而设计的。这些消息(WM_CHARWM_KEYDOWN 等)是通知,当键盘输入被接收、处理并转发给适当的接收者时,由较低级别的子系统发出。自己发送或发布这些消息就像是在恶作剧地打电话给某人。

SendKeys(与所有其他输入合成器方法一样,包括明确设计用于合成键盘输入的 SendInput 函数,并且至少在某些实现中是 SendKeys 在引擎盖下实际使用的)仅在窗口您希望接收键盘输入具有焦点。在 Windows 中,只有焦点(活动)窗口接收输入事件。

所以SendKeys 可能是你想要让它工作的方法(或者 P/Invoking SendInput 及其所有相关结构),但你确实需要尊重警告收件人窗口必须拥有焦点。否则,它什么也得不到。

从您的示例代码看来,您正在尝试使用 SetForegroundWindow 函数来满足此先决条件。不幸的是,您传递给它的是一个无效值,并且没有进行任何可能提醒您注意此错误的错误检查。具体来说,这段代码是错误的:

IntPtr ptrOBS = proc.Handle;//this works properly, proc is instantiated properly
SetForegroundWindow(ptrOBS); // WRONG, ptrOBS is not a window handle

即使我相信你 ptrOBS 被正确初始化,这也使它成为 process 的有效句柄,这与 window 的有效句柄完全不同/em>。除了明显的名义差异之外,进程可以有多个窗口,并且只有一个窗口可以具有焦点(即“在前台”)。

在调用SetForegroundWindow 之前,您需要获取特定窗口的句柄,并且鉴于我们知道一个进程可以有多个窗口,这可能会很棘手。您需要一些可靠的方法来确定您想要的窗口。许多人通过将窗口名称硬编码为字符串来实现这一点,这在重新编译目标应用程序并且此实现细节发生变化之前效果很好。我能想到的唯一防弹方法是让用户单击目标窗口和您的代码来检索当前位于鼠标指针下的窗口句柄。

当然,所有这些都假设您已经遵守了对使用 SetForegroundWindow 的限制,在链接的 SDK 文档的“备注”部分中列举了这些限制。

【讨论】:

  • IntPtr ptrOBS = FindWindow(null, "Open Broadcaster Software v0.472b");也不起作用,我假设 FindWindow 确实返回实际的窗口句柄,而不是进程句柄?但感谢您指出我需要关注和改变的事情。我现在将继续使用真正的窗口句柄来敲击 SendKeys 方法。
  • @AHeinen 你确认FindWindow实际上返回了一个句柄吗?正确的手柄?使用 Spy++ 通过将其与您感兴趣的窗口的实际句柄进行比较来验证这一点。
  • 它确实返回了应用程序的实际父窗口。我还尝试硬编码子窗口的 IntPtr hex=>decimal 值,该子窗口包含我想用按键(热键按下的按钮)运行的操作。使用任何一个/两个窗口都不起作用。我尝试实现 SetFocus(将通过我的尝试更新 OP)
【解决方案2】:

为了让它发挥作用,需要进行大量的试验和错误
这是我之前发布的一些代码,您可能想尝试一下(并且附加了一些更多信息)...

Pinvoke SetFocus to a particular control

尝试首先设置焦点(使用提到的机制) - 然后使用 SendKeysSendInput

这里是SendInput的一些详细代码...

How to send a string to other application including Microsoft Word

【讨论】:

  • 您是否设法在目标窗口中的任何内容上使用SetFocus - 使用我建议的内容(必须严格遵守) - 这是第一步。我有很多次工作。但是,某些应用程序是特定的。如果有帮助,还可以尝试提升您的应用(以管理员身份)。
  • 或尝试发布您制作的最终代码(使用该方法) - 我将复制粘贴并在此处运行(调整)并尝试看看如果时间允许
  • 我试过的setfocus实现在上面;你想让我发布我的 win32 api 函数声明还是只调用 setfocus 然后 sendkeys?
  • 最好的完整复制品(只需减少到最小 - 例如尝试使用记事本或其他东西) - 这是最容易使用的(常见示例)
  • 我明白了 :) 我一定会看看(当我找到一些时间,稍后或明天) - 告诉我你是否尝试过任何其他程序 - 比如 w/我提到的记事本?我认为您在发送密钥方面做错了 - 这是一个棘手的部分
猜你喜欢
  • 2011-08-04
  • 1970-01-01
  • 2011-09-30
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多