使用sendkey函数在C#中将键盘键发送到浏览器

Sending keyboard key to browser in C# using sendkey function

您好,我正在尝试使用下面的代码向浏览器发送密钥,新的 chrome window 为我打开,但它不会向浏览器发送密钥。

调试时发现chrome进程没有任何标题名称我该如何解决这个问题?

 Process.Start("chrome.exe", "https://labs.sketchfab.com/sculptfab/");
        System.Threading.Thread.Sleep(2000);
        foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
        {
            if (p.ProcessName == "chrome" && p.MainWindowTitle == "SculptFab - SculptGL + Sketchfab" &&
                p.MainWindowHandle != IntPtr.Zero)
            {
                System.Threading.Thread.Sleep(2000);
                for (int i = 0; i < 50; i++)
                {
                    KeyHandle.SetForeGround(p.MainWindowHandle);
                    SendKeys.Send("s");
                    System.Threading.Thread.Sleep(2000);
                }

            }
        }

在上面提到的页面中,当在键盘上按下 "S" 时,它会缩小 object 我也想使用我的 C# 代码实现这个

您可以创建 Process 的新实例,用它来发送您的击键。有关详细信息,请参阅 。

更新

我做了一些研究,似乎 Chrome 实际上对 SendKeys.Send 方法没有反应。但是,您可以使用 Windows API 调用 SendMessage 函数并将 Keydown/-up 信号发送到 window。这是在 Chrome:

中使用的简单包装器
public class ChromeWrapper
{
    // you might as well use those methods from your helper class
    [DllImport("User32.dll")]
    private static extern int SetForegroundWindow(IntPtr point);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    // the keystroke signals. you can look them up at the msdn pages
    private static uint WM_KEYDOWN = 0x100, WM_KEYUP = 0x101;

    // the reference to the chrome process
    private Process chromeProcess;
    
    public ChromeWrapper(string url)
    {
        // i'm using the process class as it gives you the MainWindowHandle by default
        chromeProcess = new Process();
        chromeProcess.StartInfo = new ProcessStartInfo("chrome.exe", url);
        chromeProcess.Start();
    }

    public void SendKey(char key)
    {
        if (chromeProcess.MainWindowHandle != IntPtr.Zero)
        {
            // send the keydown signal
            SendMessage(chromeProcess.MainWindowHandle, ChromeWrapper.WM_KEYDOWN, (IntPtr)key, IntPtr.Zero);
            
            // give the process some time to "realize" the keystroke
            System.Threading.Thread.Sleep(100); 

            // send the keyup signal
            SendMessage(chromeProcess.MainWindowHandle, ChromeWrapper.WM_KEYUP, (IntPtr)key, IntPtr.Zero);
        }
    }
}

使用这个 class 非常简单:

ChromeWrapper chrome = new ChromeWrapper("https://labs.sketchfab.com/sculptfab/");
System.Threading.Thread.Sleep(5000);

chrome.SendKey('S');

适用于我的机器™(Windows 8.1 Pro N,Google Chrome 42)。

附加信息

此解决方案仅在还没有 Chrome 运行 时才有效,因为 Chrome 只会将新的 URL 发送到它的主进程,然后主进程会打开它。因此,要么事先关闭其他 Chrome 个实例,要么在使用 Process.GetProcesses

找到的进程上使用 SendMessage 方法

这是 ChromeWrapper 的更新版本:

  • 在 chrome 已经打开时也有效(附加到现有的 chrome window
  • 在计算机锁定时工作(不同于SendKeys.SendWait)

.

public class ChromeWrapper
{
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    // the keystroke signals. you can look them up at the msdn pages
    private static uint WM_KEYDOWN = 0x100, WM_KEYUP = 0x101;

    // the reference to the chrome process
    private Process chromeProcess;

    public ChromeWrapper(string url)
    {
        Process.Start("chrome.exe", url); //no need to keep reference to this process, because if chrome is already opened, this is NOT the correct reference.
        Thread.Sleep(600);

        Process[] procsChrome = Process.GetProcessesByName("chrome");
        foreach (Process chrome in procsChrome)
        {
            if (chrome.MainWindowHandle == IntPtr.Zero)// the chrome process must have a window
                continue;
            chromeProcess = chrome; //now you have a handle to the main chrome (either a new one or the one that was already open).
            return;
        }
    }

    public void SendKey(char key)
    {
        try
        {
            if (chromeProcess.MainWindowHandle != IntPtr.Zero)
            {
                // send the keydown signal
                SendMessage(chromeProcess.MainWindowHandle, ChromeWrapper.WM_KEYDOWN, (IntPtr)key, IntPtr.Zero);
                // give the process some time to "realize" the keystroke
                Thread.Sleep(30); //On my system it works fine without this Sleep.
                // send the keyup signal
                SendMessage(chromeProcess.MainWindowHandle, ChromeWrapper.WM_KEYUP, (IntPtr)key, IntPtr.Zero);
            }
        }
        catch (Exception e) //without the GetProcessesByName you'd get an exception.
        {
        }
    }
}

我如下使用它,用于发送 Tab 和 Enter 键。

ChromeWrapper chrome = new ChromeWrapper(@"https://whosebug.com");
Thread.Sleep(300);
chrome.SendKey((char)9);// tab
chrome.SendKey((char)13);//enter