将键击发送到程序,即使它在后台使用c#

我想将关键笔划发送到程序,即使它在后台运行。 但我只能这样做NOTEPAD,

[DllImport("user32.dll")] protected static extern byte VkKeyScan(char ch); [DllImport("user32.dll", SetLastError = true)] protected static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.Dll", EntryPoint = "PostMessageA")] protected static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam); 

 char Key = // key value to send IntPtr hWnd = FindWindowEx(_handle, IntPtr.Zero, "edit", null); // _handle is the windows handle of the program (here its notepad) PostMessage(hWnd, WM_KEYDOWN, VkKeyScan(Key), 0); 

但对于所有其他应用程序,我不能发送按键,如果它在后台。 因为我不知道该程序的lpszClass (我认为这是该程序中输入区域的userControl名称。对于NotePad,它是"edit" 。我发现这个冲浪的互联网)。

对于所有其他应用程序我正在做的是,将应用程序放到前台,然后发送密钥,然后再次获取我的程序前景。 我需要我的程序始终作为前台运行。

 [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); 

 SetForegroundWindow(_handle); // _handle is the windows handle of the program System.Threading.Thread.Sleep(50); // Waiting few milliseconds till application coming to foreground. wsh.SendKeys(Key.ToString(), ref wait); // wsh is WshShellClass wsh= new WshShellClass(); SetForegroundWindow(_mainHandle); // _mainHandle is the windows handle of my application 

但这种方式不起作用。 一些关键错过了,程序前景 – >背景 – > foregound->背景……喜欢它的舞蹈……

如果在后台运行,如何将密钥发送到其他应用程序。 或者有任何方法/来源找到程序的lpszClass

对不起,如果我错过了任何必要的信息。 这是一个很大的应用程序。 我这里只发布了必需的部分。 如果有人需要任何其他信息,请询问。

您可以使用诸如WinSpy ++之类的检查工具来计算程序的lpszClass 。 它为您提供了一个十字准线,您可以拖动并定位在所需的控件上。 这能够轻松地为我提供记事本的“编辑”类名称。

如果不能正常工作,请单击WinSpy ++右下角的“更多>>”按钮,然后单击“定位”按钮查看控件层次结构; 您可能需要将WM_KEYDOWN消息发布到父控件或子控件之一。

我想你需要让后台程序通过win32函数SetWindowsHookEx()安装一个低级键盘钩子。

这是SetWindowsHookEX()的MSDN文档

http://msdn.microsoft.com/en-us/library/ms644990(v=vs.85).aspx

以下是关于如何从C#执行此操作的知识库文章

http://support.microsoft.com/kb/318804

本文也详细介绍了一下: http : //www.codeguru.com/columns/vb/article.php/c4829

我希望您的应用程序会被各种间谍软件/防病毒软件用作键盘记录器。

祝好运。