Page 1 of 1

Send keypress to inactive Brave browser (chromium, change Tab hotkey for inactive browser window)

Posted: 20 Apr 2024, 10:50
by emp00
Dear Team, I'm trying to set up a simple hotkey-script to send Ctrl-1 to a running but inactive Brave browser instance in order to activate Tab No. 1.

Found out that a single ControlSend does not work in such cases but here I found the following v1 script: "This is the seamless way to do it in Chrome and Edge and presumably other Chromium browsers; it doesn't change focus or the active window. I've been using this for years to send Win+Alt+x combinations to YouTube TV running in inactive windows."
(Note that I chancged "YTTVWinTitle" for my case to "ahk_exe brave.exe" since I just want to send the hotkey to "any" Brave instance, knowing the Ctrl-1 hotkey works in all tabs)

Code: Select all

ControlGet, ctrlID, Hwnd, , Chrome_RenderWidgetHostHWND1, ahk_exe brave.exe
if (ctrlID)
{
    ControlFocus, , ahk_id %ctrlID%
    ControlSend, Chrome_RenderWidgetHostHWND1, ^{1}, ahk_exe brave.exe
}
I converted this to v2 with the Quickconverter but it throws "Error: Missing a required parameter." at the ControlFocus line ...
Can somebody help fixing this? I have seen that in v2 ControFocus needs the "Control" parameter, apparently this changed from v1?
Maybe a better solution works for v2, only boundary condition I have is -> it should not change focus of the active window...

Code: Select all

ctrlID := ControlGetHwnd("Chrome_RenderWidgetHostHWND1", "ahk_exe brave.exe")
if (ctrlID)
{
    ControlFocus(, "ahk_id " ctrlID)
    ControlSend("^{1}", "Chrome_RenderWidgetHostHWND1", "ahk_exe brave.exe")
}

Re: Send keypress to inactive Brave browser (chromium, change Tab hotkey for inactive browser window)

Posted: 20 Apr 2024, 11:53
by mikeyww
Apparently this changed from v1?
You need not wonder, because AHK comes with documentation that you can examine.
Control: The control's ClassNN, text or HWND, or an object with a Hwnd property. For details, see The Control Parameter.
Source: ControlFocus - Syntax & Usage | AutoHotkey v2
To get a window's controls, you can use :arrow: WinGetControls.

Re: Send keypress to inactive Brave browser (chromium, change Tab hotkey for inactive browser window)

Posted: 20 Apr 2024, 12:39
by emp00
mikeyww wrote:
20 Apr 2024, 11:53
controls, you can use :arrow: WinGetControls.
Ok, using WinGetControls I found that ahk_exe brave.exe has the controls "Chrome_RenderWidgetHostHWND1", "Chrome_RenderWidgetHostHWND2" etc.; Using this information I added "Chrome_RenderWidgetHostHWND1" to the script but now it throws Error: Target control not found. for the ControlFocus line. What am I doing wrong? Thanks for your help.

Code: Select all

ctrlID := ControlGetHwnd("Chrome_RenderWidgetHostHWND1", "ahk_exe brave.exe")
if (ctrlID)
{
    ControlFocus("Chrome_RenderWidgetHostHWND1", "ahk_id " ctrlID)
    ControlSend("^{1}", "Chrome_RenderWidgetHostHWND1", "ahk_exe brave.exe")
}

Re: Send keypress to inactive Brave browser (chromium, change Tab hotkey for inactive browser window)

Posted: 20 Apr 2024, 12:45
by emp00
I made it !! :-)

This does the trick with "just" two lines - looks a little bit overdone, but it works! Can this be further simplified to just one line?

Code: Select all

ControlFocus("Chrome_RenderWidgetHostHWND1", "ahk_exe brave.exe")
ControlSend("^{1}", "Chrome_RenderWidgetHostHWND1", "ahk_exe brave.exe")

Re: Send keypress to inactive Brave browser (chromium, change Tab hotkey for inactive browser window)

Posted: 20 Apr 2024, 14:48
by emp00
Unfortunately the above was NOT working reliably - ControlSend obviously has frequent problems with inactive chromium windows :-(

In the end I created this quite complex script for a simple task - this now works, but it flickers the windows due to the WinActivate switching action between two windows. Using the SetSystemCursor and RestoreCursor scripts was helpful removing the mouse flickering back & forth, but the WinActivate window bar flickering remains. Not ideal. If anybody has a better idea, maybe a workaround which makes ControlSend work reliable with inactive brave / chrome / chromium browser windows, pls let me know.

Code: Select all

SavedActiveWindowID := WinActive("A") ; Save active window
CoordMode "Mouse", "Screen"
MouseGetPos &SavedMouseXPos, &SavedMouseYPos ; Save mouse position
	
If WinExist("ahk_exe brave.exe")
{
	SetSystemCursor() ; Briefly deactivate the mouse cursor to avoid flickering appearance on the brave window
	WinActivate("ahk_exe brave.exe")
	Send("^{1}")
	Sleep 100 ; Needs a short sleep for chromium/brave to register and execute the keypress
	WinActivate("ahk_id " . SavedActiveWindowID) ; Returns focus to initially active window
	RestoreCursor() ; Restore mouse cursor
}
Else
	MsgBox("ERROR: Brave browser window not found!`n`nahk_exe brave.exe",, "O IconX 262144 T3")
	
MouseMove SavedMouseXPos, SavedMouseYPos, 0 ; Restore mouse position, speed 0 = fastest	
ExitApp

Re: Send keypress to inactive Brave browser (chromium, change Tab hotkey for inactive browser window)  Topic is solved

Posted: 20 Apr 2024, 15:36
by Noitalommi_2
Hi.

@emp00
Please try this script, it sends Ctrl+1 to all existing Brave Browser windows without losing the focus of the current window.

Code: Select all

#Requires AutoHotkey 2.0
#SingleInstance

F1:: {

	Hwnds := WinGetList("ahk_exe brave.exe")
	For , Hwnd in Hwnds {

		PostMessage(WM_ACTIVATE := 0x0006 , 1, Hwnd,, "ahk_id" Hwnd)
		ControlSend("^{1}",, "ahk_id" Hwnd)
	}
}

Re: Send keypress to inactive Brave browser (chromium, change Tab hotkey for inactive browser window)

Posted: 21 Apr 2024, 05:24
by emp00
Noitalommi_2 wrote:
20 Apr 2024, 15:36
Please try this script, it sends Ctrl+1 to all existing Brave Browser windows without losing the focus of the current window.
Thank you very much! It works, so far - tested under several conditions, seems to be reliable! THANKS!!

Fyi, I checked the Hwnds array, at least in my case (Brave running with around 15 Tabs open) it has only one single entry and it's the "ahk_id". So the For loop may be overkill - however, I found it's relevant in case multiple Brave browser windows are running - then it indeed sends Ctrl-1 to all Brave browser windows. Thanks for even thinking of this, excellent solution!