使用sendkey函数在C#中将键盘键发送到浏览器 [英] Sending keyboard key to browser in C# using sendkey function

查看:108
本文介绍了使用sendkey函数在C#中将键盘键发送到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello i am trying to send a key to browser using the code below, the new chrome window opens for me but it does not send the key to the browser.

我调试的时候发现chrome进程没有标题名怎么解决这个问题?

When i debugged i found out chrome process does not have any title name how can i solve this problem?

 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# 代码实现这一点

In the above mentioned page when "S" is pressed on the keyboard it zooms out of the object i want too achieve this using my C# code

推荐答案

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

You can create a new instance of Process use it to send your keystrokes. See https://stackoverflow.com/a/12892316/2058898 for further information.

我做了一些研究,看来 Chrome 实际上对 SendKeys.Send 方法没有反应.但是,您可以使用 Windows API 调用 SendMessage 函数并将 Keydown/-up 信号发送到窗口.这是在 Chrome 中使用的简单包装器:

I've done some researching and it seems Chrome does in fact not react to the SendKeys.Send method. However you can use the Windows API to call the SendMessage function and send Keydown/-up signals to the window. Here's a simple wrapper for using in 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).

Works on my machine™ (Windows 8.1 Pro N, Google Chrome 42).

其他信息

此解决方案仅适用于尚未运行 Chrome 的情况,因为 Chrome 只会将新 URL 发送到它的主进程,然后主进程会打开它.因此,要么事先关闭其他 Chrome 实例,要么在使用 Process.GetProcesses

This solution only works if there's no Chrome running yet, as Chrome only sends the new URL to it's main process which opens it then. So either close other Chrome instances beforehand or use the SendMessage method on the process you found using Process.GetProcesses

这篇关于使用sendkey函数在C#中将键盘键发送到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆