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 ----
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; ip[1].ki.wScan = ip[2].ki.wScan = 0x0F;
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 == {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 -
void clicktabs()
{
SetForegroundWindow(hwnd);
for(int count = 1;;count++)
{
if (count == {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.