将alt-g发送到chrome窗口,在tradingview中打开goto对话



我正在尝试打开一个tradingview url,然后调用一个goto日期。alt-g。我找到了这根我认为可能有用的线。按下ALT+KEY时处理KeyDown事件。下面有以下代码。(我通过查看另一个随机线程添加了SendAltKey。(。

第一个例子是有效的。第二次尝试在tradingview中调用alt-g并没有成功。没有url日期查询字符串。有什么想法吗?

ChromeWrapper chrome = new ChromeWrapper(@"https://stackoverflow.com");
Thread.Sleep(1000);
chrome.SendKey((char)9);// tab
chrome.SendKey((char)13);//enter
// open trading view and bring up goto date dialogue. 
// end game to navigate to an actual date, but cannot get past this point.
chrome = new ChromeWrapper(@"https://www.tradingview.com/chart/?symbol=NASDAQ%3AMSFT");
Thread.Sleep(10000);
chrome.SendAltKey('G'); // doesn't work.
SendKeys.Send("%G"); // doesn't work either.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Meh
{
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
public Process chromeProcess;
public ChromeWrapper(string url)
{
chromeProcess = new Process();
chromeProcess.StartInfo = new ProcessStartInfo("chrome.exe", url);
chromeProcess.StartInfo.UseShellExecute = true;
chromeProcess.Start(); //no need to keep reference to this process, because if chrome is already opened, this is NOT the correct reference.
Thread.Sleep(600); //without this behavior is altered (tap key presses operate on other objects on the page)

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.
{
}
}
public void SendAltKey(char key)
{
try
{
if (chromeProcess.MainWindowHandle != IntPtr.Zero)
{
uint lparam = (0x01 << 28);
SendMessage(chromeProcess.MainWindowHandle, ChromeWrapper.WM_KEYDOWN, (IntPtr)key, (IntPtr) lparam);
Thread.Sleep(30); 
SendMessage(chromeProcess.MainWindowHandle, ChromeWrapper.WM_KEYUP, (IntPtr)key, (IntPtr) lparam);
}
}
catch (Exception e) //without the GetProcessesByName you'd get an exception.
{
}
}
}
}

如果窗口处于焦点位置,则可以使用:

System.Windows.Forms.SendKeys.SendWait("%(g)");

则CCD_ 1是alt,因此CCD_。

如果这不起作用,或者你不想使用System.Windows.Forms,你可以寻找Robot.cs

最新更新