将击键发送到另一个应用程序
问题描述:
嗨!
我正在尝试向c#中的应用程序发送一些击键,我在论坛等上找到了一些有用的信息,但是我无法使它起作用,您能告诉我我做错了什么吗?这是我的代码:
Hi!
I''m trying to send some keystrokes to an application in c#, i found some usefull information on forums and such, but I couldn''t make it work, can you tell me what i did wrong ? this is my code:
[DllImport("User32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr hWnd);
public void Procese()
{
Process[] processlist = Process.GetProcessesByName("winrar");
foreach (Process theprocess in processlist)
{
IntPtr ptrWIRAR = theprocess.Handle;
SetForegroundWindow(ptrWIRAR);
SendKeys.Send("{F1}");
}
}
这是一个测试,如果我可以完成这项工作,那么它将在每个过程中都可以使用.
请帮助!
this is a test, if i can make this work, then it will work fore every process.
pleas help!
答
说我无法使它工作"并不能告诉我们蹲下.您是否已在调试器下运行它以确保它实际上正在找到所需的进程,并且foreach代码是否正常工作?
Saying "I couldn''t make it work" doesn''t tell us squat. Have you run it under the debugger to make sure it''s actually FINDING the desired process, and that the foreach code is working?
此Codeproject文章将为您提供帮助:
将击键发送到C#中的另一个应用程序 [
This Codeproject article will help you:
Sending Keystrokes to another Application in C#[^]
SendKeys
的应用非常有限.发送键盘和鼠标输入的最可靠方法是使用低级Windows APISendInput
,请参阅 ^ ].实际上,这实际上是键盘和鼠标驱动程序所做的.
不过,我需要警告:在不同处理程序之间进行协作是一件坏事.
将其用于UI开发甚至更糟.这只是一个hack,仅此而已,不再是可靠的,也不是可支持的. 我建议您一定要通过模拟用户输入来避免进程之间的通信.—SA
The application ofSendKeys
is very limited. The most reliable way to send keyboard and mouse input is using low-level Windows APISendInput
, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^]. This is practically what keyboard and mouse drivers ultimately do.
I need to warn you though: using this for collaboration between different processed is bad thing.
Using it for UI development is even worse. This is just a hack, no more, not reliable, not supportable. I would advise to avoid communication between processes via simulation of user input by all means.—SA