Click here to Skip to main content
15,888,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am writing a code in C/C++ with Win32API which Automates Chrome(I know there is Selenium and I used that but there is BOT detection available. Just forget about slenium).In my code There are extra features too ie.-If you have multiple Chrome Profile you can chose to run how many Profiles to Run at Once. To change the Tabs in Google Chrome they have the Shortcut Key Ctrl+TAB. I want to Simulate that.

What I have tried:

The Problems ->
1.To Run Multiple Profiles at Once.
2.Detecting The Window Handles of Every Profile.
3.Automating Certain Keyboard Events.

I have solved Problem 1 and 2.

Even Problem 3 is easy.To automate Chrome Tab changes I have done something like this ----
C++
void clicktabs()
{
    SetForegroundWindow(hwnd);
    INPUT ip[4];
    ip[0] = ip[1] = ip[2] = ip[3] = { 0 };
    
    ip[0].type = ip[1].type = ip[2].type = ip[3].type = INPUT_KEYBOARD;
    ip[0].ki.wVk = ip[3].ki.wVk = VK_CONTROL;
    
    ip[0].ki.wScan = ip[3].ki.wScan = 0x1D; //scancode of CTRL for my Keyboard
    ip[1].ki.wScan = ip[2].ki.wScan = 0x0F;//scancode of TAB for my Keyboard
    //You can also check for yours by observing the lParam Parameter of WndProc().bit 16 to 23 of lParam is your scan code for that particular key.Though it does not matter.

    ip[3].ki.dwFlags = ip[2].ki.dwFlags = KEYEVENTF_KEYUP;
    ip[1].ki.wVk = ip[2].ki.wVk = VK_TAB;
    
    for(int count = 1;;count++)
    {
    	if (count == //number of tabs you want to open)
    	{count = 0;}
            SendInput(4, ip, sizeof(INPUT));
            Sleep(2000);
    }
}

It is working just fine to press Ctrl+TAB at once.And I am successful to automate TAB changes in Chrome.
Main Problem ---------->

SendInput() work on the Focused window. If i change my Focus to other window, the chrome TAB change automation stops. To solve it I used PostMessage. But that does not work.

I inspect Chrome Window by Spy++ while pressing ctrl+tab by myself on my keyboard. And it generates-----

1.P WM_KEYDOWN nVirtkey:VK_CONTROL cRepeat: 1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:0 fUP:0
2.P WM_KEYDOWN nVirtkey:VK_TAB cRepeat: 1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUP:0
3.P WM_KEYUP nVirtkey:VK_TAB cRepeat: 1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:1 fUP:1
4.P WM_KEYUP nVirtkey:VK_CONTROL cRepeat: 1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:1 fUP:1

(Here all the preceeding 'P' means the message is Posted by PostMessage() Note-For normal keydown messages system PostsMessage not SendMessage().You can see it here - Message Codes - Visual Studio | Microsoft Docs[^] )

When I am using SendInput() function and checking browser window with Spy++ it exactly generates the same sequence of Messages as the real keyboard press do.But when I am using PostMessage() like this -

C++
void clicktabs()
    {
          SetForegroundWindow(hwnd);
          for(int count = 1;;count++)
    	{
    		if (count == //number of tabs you want to open)
    		{count = 0;}
    		LPARAM lparam_ctrlkey, lparam_tabkey;
    		lparam_ctrlkey = 1 | (1 << 16) | (1 << 18) | (1 << 19) | (1 << 20);
    		PostMessage(hwnd, WM_KEYDOWN, VK_CONTROL, lparam_ctrlkey);
    		lparam_tabkey = 1| (1 << 16) | (1 << 17) | (1 << 18) | (1 << 19);
    		PostMessage(hwnd, WM_KEYDOWN, VK_TAB, lparam_tabkey);
    		lparam_tabkey = lparam_tabkey|(1<<30)|(1<<31);\\\\\\stteing up the lparams
    		PostMessage(hwnd, WM_KEYUP, VK_TAB, lparam_tabkey);
    		lparam_ctrlkey = lparam_ctrlkey | (1 << 30) | (1 << 31);
    		PostMessage(hwnd, WM_KEYUP, VK_CONTROL, lparam_ctrlkey);
    		Sleep(2000);
              }
    }

and Checking with Spy++.It generates one extra message(see line 3)-----
1.P WM_KEYDOWN nVirtkey:VK_CONTROL cRepeat: 1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:0 fUP:0
2.P WM_KEYDOWN nVirtkey:VK_TAB cRepeat: 1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUP:0
3.P WM_CHAR chCharCode:'9' cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0
4.P WM_KEYUP nVirtkey:VK_TAB cRepeat: 1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:1 fUP:1
5.P WM_KEYUP nVirtkey:VK_CONTROL cRepeat: 1 ScanCode:1D fExtended:0 fAltDown:0 fRepeat:1 fUP:1

At line 3 it's Generating WM_CHAR for TAB key.ASCII code for TAB key is 0x09 or 9(decimal).And Here PostMessage() fails.

Please help me to solve this.
Posted
Updated 4-Dec-20 2:29am
v3
Comments
[no name] 5-Dec-20 6:06am    
Hi,

I don't know what you are working on but using PostMessage to for automation is bound to fail. I would suggest that you take a look at Microsoft Active Accessibility which is designed to assist disabled people play games and use the computer.

https://docs.microsoft.com/en-us/windows/win32/winauto/microsoft-active-accessibility

Chromium supports these COM interfaces but you need to enable it in the browser:
https://www.chromium.org/developers/design-documents/accessibility
Aniket945 5-Dec-20 9:04am    
PostMessage() is doing great if I send mouse clicks on the tabs even when chrome is minimized.But it is failing to press ctrl+tab because of the extra char'9' message it is generating.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900