views:

101

answers:

2

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  A: 

Take a look into the SendKeys class.

Oliver
+4  A: 

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");
IntPtr ptrFF = proc.Handle;
SetForegroundWindow(ptrFF);
SendKeys.SendWait("{F1}");
Kyle Rozendo
and by process name?
Or Betzalel
@Or Betzalel - Updated with process name.
Kyle Rozendo
Thanks, figured it out
Or Betzalel