从C#应用程序发送击键到Java应用程序 - 奇怪的行为? [英] Sending keystrokes from a C# application to a Java application - strange behaviour?

查看:267
本文介绍了从C#应用程序发送击键到Java应用程序 - 奇怪的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的C#程序发送键击到Java应用程序



有关sendig键的代码是:



 私人无效SendKeysToWindow(字符串WindowName,串KeysToSend)
{
的IntPtr的hWnd = FindWindow函数(NULL,WindowName);
ShowWindowAsync(HWND,SW_SHOWNORMAL);
SetForegroundWindow(HWND);
SendKeys.Send(KeysToSend);
}

此代码工作正常使用的所有程序,除了与Java应用程序,我:M tyring控制



例如,如果我创建了一个按钮,如下因素代码:

  SendKeysToWindow(Java应用程序的窗口名,{F2}); 
SendKeysToWindow(Popoup窗口名称,123);

这发送F2主程序窗口,另一个窗口弹出,第二SendKeysToWindow命令发送123来表示窗口。
这是它是如何预期的工作,而这是与所有其他程序的情况下。



然而,当我这些命令发送给Java程序,发生以下情况:第一SendKeysToWindow命令被执行罚款(出现的弹出窗口),但它不发送123到窗口



如果是按按钮再次,123发送到弹出窗口,并打开另一个popoup窗口。



如果我创建了两个SendKeysToWindow命令两个单独的按钮,然后按之后他们彼此,这两个命令执行罚款。



有什么可以成为probem?



感谢您的帮助在advanvce,它真的快把我逼疯了。



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


< DIV CLASS =h2_lin>解决方案

一些试验和错误后,下面的代码似乎很好地工作:

 私人无效SendKeysToWindow(字符串WindowName,串KeysToSend)
{
的IntPtr的hWnd = FindWindow函数(NULL,WindowName);
的ShowWindow(HWND,SW_SHOWNORMAL);
SetForegroundWindow(HWND);
Thread.sleep代码(50);
SendKeys.SendWait(KeysToSend);
}


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.

解决方案

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);           
    }

这篇关于从C#应用程序发送击键到Java应用程序 - 奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆