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

标签 c# process sendkeys

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

当我调试时,我发现 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”时,它会缩小我想要使用我的 C# 代码实现的对象

最佳答案

您可以创建一个新的 Process 实例,用它来发送您的击键。参见 https://stackoverflow.com/a/12892316/2058898了解更多信息。

更新

我做了一些研究,似乎 Chrome 实际上对 SendKeys.Send 方法没有反应。但是,您可以使用 Windows API 调用 SendMessage 函数并将 Keydown/-up 信号发送到窗口。这是一个在 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);
        }
    }
}

使用这个类非常简单:

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 方法

关于c# - 使用sendkey函数在C#中将键盘键发送到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30156913/

相关文章:

c# - 如何使用发送键将 Ctrl+Shift+F1 发送到应用程序

c# - 为什么从浏览器调用时 MySQL 结果正常,但从 C# 调用时为空或 NULL?

c++ - 在c++中,不等待不等于在后台运行?

c - 如何在 C 中创建一个在父进程死亡后运行的进程?

c# - 由另一个产生的调试进程

python - 如何使用 python 脚本发送键盘命令(保持、释放、同时)?

c# - 即使最小化也将 key 发送到表单

c# - puppeetersharp 中的单页 PDF

c# - 为 LINQ 伪造 IGrouping

c# - IAsyncResult 引用