Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,382 ![United States United States](/Content/images/flags/US.png) Location: Tampa, FL Thanks: 28 times Was thanked: 430 time(s) in 363 post(s)
|
If you're sending keystrokes or mouse events to the window, it will need to remain the foreground window as that is the only window which receives keyboard and mouse input. While it is technically possible to send events via windows messages, it's extremely complicated and not very reliable/consistent; sometimes not even possible depending on the application. For example, this script should work to show the Chrome Bookmarks bar (Control+Shift+B): Code:var chromeWindow = action.Window;
//Control Down
chromeWindow.PostMessageObj(0x0100, 0x11, 0x1D0001);
sp.Sleep(10);
//Shift Down
chromeWindow.PostMessageObj(0x0100, 0x10, 0x2A0001);
sp.Sleep(10);
//b Down
chromeWindow.PostMessageObj(0x0100, 0x42, 0x300001);
sp.Sleep(10);
//b Up
chromeWindow.PostMessageObj(0x0101, 0x42, 0xC0300001);
sp.Sleep(10);
//Shift Up
chromeWindow.PostMessageObj(0x0101, 0x10, 0xC02A0001);
sp.Sleep(10);
//Control Up
chromeWindow.PostMessageObj(0x0101, 0x11, 0xC01D0001);
However, because of how TranslateMessage works, it doesn't translate the "B" to the correct char code, because the system does not see the Control and Shift keys as being pressed down. So instead of Chrome receiving 2 for the WM_CHAR message, it receives 98, which is the same value it receives when you just press the letter "B" on the keyboard. But this will send the text " test" to Chrome, if the window is active and a text area has focus: Code:var chromeWindow = action.Window;
//t Down
chromeWindow.PostMessageObj(0x0100, 0x54, 0x140001);
sp.Sleep(10);
//t Up
chromeWindow.PostMessageObj(0x0101, 0x54, 0xC0140001);
sp.Sleep(10);
//e Down
chromeWindow.PostMessageObj(0x0100, 0x45, 0x0120001);
sp.Sleep(10);
//e Up
chromeWindow.PostMessageObj(0x0101, 0x45, 0xC0120001);
sp.Sleep(10);
//s Down
chromeWindow.PostMessageObj(0x0100, 0x53, 0x01F0001);
sp.Sleep(10);
//s Up
chromeWindow.PostMessageObj(0x0101, 0x53, 0xC01F0001);
sp.Sleep(10);
//t Down
chromeWindow.PostMessageObj(0x0100, 0x54, 0x140001);
sp.Sleep(10);
//t Up
chromeWindow.PostMessageObj(0x0101, 0x54, 0xC0140001);
sp.Sleep(10);
Then I thought, maybe I could send a space message to Chrome even if it wasn't the active window, since that only scrolls the page and doesn't require a text area to have focus to receive the input: Code:var chromeWindow = action.Window;
//Space Down
chromeWindow.PostMessageObj(0x0100, 0x20, 0x390001);
sp.Sleep(10);
//Space Up
chromeWindow.PostMessageObj(0x0101, 0x20, 0xC0390001);
sp.Sleep(10);
But that did not work either, it only works when Chrome is active. So you see, while it sometimes can be possible for some applications, very often it doesn't work and is hardly worth the amount of time it takes to try. Trust me, I've spent a lot of time over the years on these types of things! There have been a few times where it worked, but it's usually rare.
|