0

I created a Process. That one has a MainWindow I want to SendKeys.Send("+F") (CTRL+F) to, but I don't know how to do this.

So how is this done?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Hedge
  • 16,142
  • 42
  • 141
  • 246
  • It sounds like every example requires a window of your created process to be brought to the foreground. – Ian Boyd Sep 24 '10 at 20:50

3 Answers3

2

For Ctrl key you need to precede the key code with ^. something like:

SendKeys.Send("^F");

Check here for more information.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
1

You'll need something like the following to set focus to an external window:

public class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    private void button1_Click(object sender, EventArgs e)
    {
        Process[] process = Process.GetProcessesByName("notepad");

        if (process.Length > 0)
            SetForegroundWindow(process[0].MainWindowHandle);
    }
}
Bernard
  • 7,908
  • 2
  • 36
  • 33
  • More answers can be found here: http://stackoverflow.com/questions/3787057/need-to-activate-a-window – Bernard Sep 24 '10 at 18:51
0

Hope the following helps. This maximized WMP then sends Ctrl+P to play the paused music:


[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd);

IntPtr handle = FindWindow(null, "Windows Media Player"); if (handle != IntPtr.Zero) { // Maximize WMP ShowWindow(handle, (uint) WindowShowStyle.Maximize); // Use SwitchToThisWindow(handle, false) OR SetForegroundWindow(handle) SetForegroundWindow(handle); // Make sure the window is brought to the froeground Thread.Sleep(200); // Use SendKeys OR SendInput API SendKeys.SendWait("^p"); // Minimize WMP ShowWindow(handle, (uint)WindowShowStyle.Minimize); }

sh_kamalh
  • 3,853
  • 4
  • 38
  • 50
  • SetForegroundWindow is very likely to fail. Check the return value. – Hans Passant Sep 24 '10 at 19:24
  • Thanks for that. I am not really expert with Windows API but I needed to create a program that resumes and pauses WMP because my first laptop didn't have a key for pause and resume and the above is really working. – sh_kamalh Sep 24 '10 at 20:01