【问题标题】:Sending keyboard key to browser in C# using sendkey function使用 sendkey 函数在 C# 中向浏览器发送键盘键
【发布时间】:2015-07-21 07:44:24
【问题描述】:

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

当我调试时发现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# 代码实现此目的的对象

【问题讨论】:

  • 为什么不用 Selenium?除了 Windows 字幕问题,由于 UAC,SendKeys.Send 在最近的 Windows 版本上不够稳定...
  • 我实际上正在使用 kinect 制作一个 C# 应用程序,它会通过手势提供输入,我认为 Selenium 不能帮助我做到这一点

标签: c# process sendkeys


【解决方案1】:

您可以创建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 方法

【讨论】:

  • 是否可以向某个 Chrome 扩展按钮发送击键来加载它?
  • 我看不出这不起作用的原因,因为这会将击键发送到窗口。您可能需要先模拟单击扩展程序的按钮才能显示它,但它应该可以工作。
  • 您需要以管理员身份运行您的 C# 代码/可执行文件才能使其与 Google Chrome 一起使用。
  • 它不适用于字母数字键,仅适用于特殊键,例如:TAB、SPACE 等。我已使用管理员运行 Visual Studio,但仍然无法使用。
  • @CosminIoniță,我在stackoverflow.com/a/20493025/2612547找到了解决方案
【解决方案2】:

这是 ChromeWrapper 的更新版本:

  • chrome 已经打开时也可以使用(附加到现有的 chrome 窗口
  • 在计算机锁定时工作(与 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://stackoverflow.com");
Thread.Sleep(300);
chrome.SendKey((char)9);// tab
chrome.SendKey((char)13);//enter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2011-08-15
    • 2010-12-31
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多