在C#/ .NET中将密钥发送到非活动应用程序

我有一个使用combobox的应用程序,其中包含当前运行的应用程序的名称。 据我所知,从msdn库,SendKeys方法只能将密钥发送到活动应用程序。 在.NET中,以某种方式可以将密钥发送到非活动应用程序吗? 或者至少在WinAPI中?

您可以使用SendMessage() API函数将键击发送到非活动窗口。

随着C#<3,可能会出现:D

不需要像你希望的那样成为活跃的窗口。

此处还有一个有用的虚拟密钥代码列表

  [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam); private void button1_Click(object sender, EventArgs e) { const int WM_SYSKEYDOWN = 0x0104; const int VK_KEY_A = 0x41; IntPtr WindowToFind = FindWindow(null, "Window Name"); //In ur case u have to write a code that translates the combobox into Virtual Key Codes. Will take time but it shouls be easy PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_KEY_A, 0); //PostMessage(WindowToFind, WM_SYSKEYDOWN, ((int)Keys.NumPad7), 0); }