views:

47

answers:

3

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?

+1  A: 

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

SendKeys.Send("^F");

Check here for more information.

Faisal Feroz
I still need to give the main-window of the Process the focus first.How to do this?
Hedge
Alt+Tab should help :-)
Faisal Feroz
+1  A: 

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
More answers can be found here: http://stackoverflow.com/questions/3787057/need-to-activate-a-window
Bernard
A: 
sh_kamalh
SetForegroundWindow is very likely to fail. Check the return value.
Hans Passant
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