Sending keystrokes to a program

早过忘川 提交于 2019-11-29 13:46:57

问题


In window form, I made a button and I'm trying to make it send F1 to a specific window (Such as FireFox, My Computer, etc...)

My questions are :

  • How do I do it by the window's name? (such as "Mozilla Firefox")
  • How do I do it by the process's name? (such as firefox.exe)

回答1:


By Window name:

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

By Process name:

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



回答2:


Take a look into the SendKeys class.



来源:https://stackoverflow.com/questions/2744111/sending-keystrokes-to-a-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!