将击键从 C# 应用程序发送到 Java 应用程序 - 奇怪的行为?

发布于 2024-08-17 13:29:11 字数 915 浏览 7 评论 0原文

我正在尝试将击键从我的 C# 程序发送到 Java 应用程序

发送键的代码是:

private void SendKeysToWindow(string WindowName, string KeysToSend)
    {
        IntPtr hWnd = FindWindow(null, WindowName);
        ShowWindowAsync(hWnd, SW_SHOWNORMAL);
        SetForegroundWindow(hWnd);
        SendKeys.Send(KeysToSend);            
    }

此代码适用于所有程序,但我试图控制的 Java 应用程序除外。

例如,如果我使用以下代码创建一个按钮:

SendKeysToWindow("Java application window name", "{F2}");
SendKeysToWindow("Popoup window name", "123");

这会将 F2 发送到主程序窗口,其中弹出另一个窗口,第二个 SendKeysToWindow 命令将“123”发送到该窗口。 这就是它的预期工作方式,所有其他程序也是如此。

但是,当我将这些命令发送到 Java 程序时,会发生以下情况:第一个 SendKeysToWindow 命令执行正常(出现弹出窗口),但它不会将“123”发送到该窗口。

如果再次按下该按钮,“123”将被发送到弹出窗口,并打开另一个弹出窗口。

如果我为两个 SendKeysToWindow 命令创建两个单独的按钮,并依次按下它们,则这两个命令都可以正常执行。

问题可能是什么?

感谢您提前提供的帮助,这真的让我发疯。

PS:我是C#初学者,所以请保持答案简单。

I'm trying to send keystrokes from my C# program to a Java application

The code for sendig keys is:

private void SendKeysToWindow(string WindowName, string KeysToSend)
    {
        IntPtr hWnd = FindWindow(null, WindowName);
        ShowWindowAsync(hWnd, SW_SHOWNORMAL);
        SetForegroundWindow(hWnd);
        SendKeys.Send(KeysToSend);            
    }

This code works fine with all programs, except with the Java application that I'm tyring to control.

For example, if I create a button with the folowing code:

SendKeysToWindow("Java application window name", "{F2}");
SendKeysToWindow("Popoup window name", "123");

This sends an F2 to the main program window, where another window pops up, and the second SendKeysToWindow command sends the "123" to that window.
This is how it is expected to work, and this is the case with all other programs.

However, when I send these commands to the Java program, the following happens: the first SendKeysToWindow command is executed fine (the popup window appears), but it does not send the "123" to that window.

If is press the button again, the "123" is sent to the popup window, and it opens another popoup window.

If I create two separate buttons for the two SendKeysToWindow command, and press them one after another, both commands execute fine.

What can be the probem?

Thanks for the help in advanvce, it's really driving me crazy.

P.S.: I'm a beginner in C#, so please keep the answer simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2024-08-24 13:29:11

经过一番尝试和错误后,以下代码似乎工作正常:

private void SendKeysToWindow(string WindowName, string KeysToSend)
    { 
        IntPtr hWnd = FindWindow(null, WindowName);            
        ShowWindow(hWnd, SW_SHOWNORMAL);
        SetForegroundWindow(hWnd);
        Thread.Sleep(50);
        SendKeys.SendWait(KeysToSend);           
    }

After some trial and error, the following code seems to work fine:

private void SendKeysToWindow(string WindowName, string KeysToSend)
    { 
        IntPtr hWnd = FindWindow(null, WindowName);            
        ShowWindow(hWnd, SW_SHOWNORMAL);
        SetForegroundWindow(hWnd);
        Thread.Sleep(50);
        SendKeys.SendWait(KeysToSend);           
    }
满天都是小星星 2024-08-24 13:29:11

听起来好像发送 {f2} 和 Java 应用程序打开弹出窗口之间可能存在延迟。

您是否尝试过检查 FindWindow 是否成功或失败?

It sounds like there might just be a delay between sending {f2} and the Java application opening the popup window.

Have you tried checking whether FindWindow succeeds or fails?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文