1

I have started a process and want to post a message like PageDown key to it.

Here is the code for running the process.

Process.Start("chrome.exe", "D:/sample.htm");
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
    if (p.ProcessName == "chrome" && p.MainWindowTitle == "sample.htm - Google Chrome")
    {
       //how to Send a pagedown key to process p
    }
}

I created following class but i don't know why it doesn't work?

class KeyHandle
{
    private static Int32 WM_KEYDOWN = 0x100;
    private static Int32 WM_KEYUP = 0x101;

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam);

    public static void SendKey(IntPtr hWnd, System.Windows.Forms.Keys key)
    {
        PostMessage(hWnd, WM_KEYUP, key, 0);
    }


}

and call it this way

foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
    if (p.ProcessName == "chrome" && p.MainWindowTitle == "sample.htm - Google Chrome")
    {
         KeyHandle.SendKey(p.MainWindowHandle, Keys.PageDown);
    }
}
saeed
  • 2,477
  • 2
  • 23
  • 40
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 18 '13 at 06:36
  • @JohnSaunders thanks for improving post! Have you got any solution for this post? – saeed Nov 18 '13 at 06:38

2 Answers2

2

I rewrite your code using SendKeys API. I test it it works well

foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
    if (p.ProcessName == "chrome" && p.MainWindowTitle == "sample.htm - Google Chrome" &&
        p.MainWindowHandle != IntPtr.Zero)
    {
        SetForegroundWindow(p.MainWindowHandle);
        SendKeys.SendWait("{PGDN}");
    }
}

To declare the function SetForegroundWindow, use :

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

List of keys

saeed
  • 2,477
  • 2
  • 23
  • 40
FArcellier
  • 304
  • 1
  • 3
0

Process.Start("chrome.exe", "D:/sample.htm"); foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) { if (p.ProcessName == "chrome" && p.MainWindowTitle == "sample.htm - Google Chrome") { KeyHandle.SendKey(p.MainWindowHandle, Keys.PageDown); } } You require this class as well

class KeyHandle { private static Int32 WM_KEYDOWN = 0x100; private static Int32 WM_KEYUP = 0x101;

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam);

public static void SendKey(IntPtr hWnd, System.Windows.Forms.Keys key)
{
    PostMessage(hWnd, WM_KEYUP, key, 0);
}

}

8bitcat
  • 2,206
  • 5
  • 30
  • 59
  • This is my answer which i provided for an other question but it does not work in win7 i post it that users test code in different platforms like winxp and vista as well – saeed Nov 18 '13 at 07:01