SeanK
SeanK

Reputation: 15

send alt-g to chrome window to open a goto dialogue in tradingview

I am trying to open a tradingview url and then invoking a goto date. alt-g. i found this thread which i thought might work. Handle the KeyDown Event when ALT+KEY is Pressed. this has the following code below. (i added SendAltKey by looking at another random thread.).

The first example works. Second trying to invoke alt-g in tradingview does not. There is no url date querystring. Any ideas?

       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.
            {
            }
        }
    }
}


Upvotes: 0

Views: 92

Answers (1)

Vinod Srivastav
Vinod Srivastav

Reputation: 4255

If the Window is in focus you can use:

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

the % is alt so %(g) will be Alt+g.

If that's doesn't work or you don't want to use System.Windows.Forms you may look for Robot.cs

Upvotes: 0

Related Questions