向程序发送击键

在窗口forms,我做了一个按钮,我试图让它发送F1到一个特定的窗口(如FireFox,我的电脑等……)

我的问题是:

  • 我怎么用窗口的名字来做? (例如“Mozilla Firefox”)
  • 我如何通过流程的名称来做到这一点? (例如firefox.exe)

按窗口名称:

[DllImport("User32.dll")] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll")] static extern int SetForegroundWindow(IntPtr hWnd); IntPtr ptrFF = FindWindow(null, "Mozilla Firefox"); SetForegroundWindow(ptrFF); SendKeys.SendWait("{F1}"); 

按流程名称:

 Process proc = Process.GetProcessesByName("firefox")[0]; IntPtr ptrFF = proc.Handle; SetForegroundWindow(ptrFF); SendKeys.SendWait("{F1}"); 

看看SendKeys类。