简体   繁体   English

如何将密钥发送到流程

[英]How to send a key to a process

I have started a process and want to post a message like PageDown key to it. 我已经开始一个过程,并想向其发布诸如PageDown键之类的消息。

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

I rewrite your code using SendKeys API. 我使用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 : 要声明函数SetForegroundWindow,请使用:

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

List of keys 按键清单

Process.Start("chrome.exe", "D:/sample.htm"); 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); foreach(System.Diagnostics.Process.GetProcesses()中的System.Diagnostics.Process p){如果(p.ProcessName ==“ chrome” && p.MainWindowTitle ==“ sample.htm-Google Chrome”){ p.MainWindowHandle,Keys.PageDown); } } You require this class as well 您也需要此类

class KeyHandle { private static Int32 WM_KEYDOWN = 0x100; class KeyHandle {private static Int32 WM_KEYDOWN = 0x100; private static Int32 WM_KEYUP = 0x101; 私有静态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);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM