Google-chrome – How to use Autohotkey to focus on an existing Google Chrome tab, not a “container” window


How can I use Autohotkey to focus on an existing Google Chrome tab, not a "container" window?


Google Chrome seems to represent each window with a container window handle, which contains one or more tabs. The tabs (at least the current one), has its own window handle. The tab window handles have window titles (which currently all end in " – Google Chrome"), whereas the container window handle itself does not. The following autohotkey code does not work as intended for Google Chrome:

if WinExist("ahk_class Chrome_WidgetWin_0")
    Run "C:\Users\vleeshue\AppData\Local\Google\Chrome\Application\chrome.exe"

This binding will focus on a Google Chrome window if it exists or will run Google Chrome. However, it will often target the container window (in Window Spy, the window title is blank). Activating the container window disallows the use of Google Chrome keyboard shortcuts. The inaccessible keyboard shortcuts include the all important ctrl+l to access the omnibar. Since I have not yet found a way to consistently activate the tab window instead of the container window, my workaround is to use the mouse, but I'd prefer to avoid that if possible.

Window Spy Screenshots

Container Window Handle

Tab Window Handle


Current Google Chrome Version: 5.0.317.2 dev

A common autohotkey binding I use is a keyboard shortcut to focus a specific application if it's already running or to run the application if it isn't running.

For example, I use this for foobar2000

If WinExist("foobar2000")
    Run "C:\Program Files (x86)\foobar2000\foobar2000.exe"

Best Answer

if WinExist("ahk_class Chrome_WindowImpl_0")
  ControlFocus, Chrome_AutocompleteEditView1
  Run "C:\Users\vleeshue\AppData\Local\Google\Chrome\Application\chrome.exe"

Should do the trick

("Chrome_AutocompleteEditView1" is the name of the omnibar control, so you could add Send ^a to select all)

Note: To get the ahk_class for your version of Chrome, e.g., ahk_class Chrome_WindowImp1-0, use the AU3_Spy.exe inside the autohotkey directory. This will allow you to find the correct ahk class for your chrome browser if the example one doesn't work.

Update: I can't reproduce, maybe it will be better with another control... To have a list of a window controls I use this code:

SetTimer, WatchCursor, 100

  MouseGetPos, , , id, control
  WinGetTitle, title, ahk_id %id%
  WinGetClass, class, ahk_id %id%
  WinGet, ControlList, ControlList, A
  ToolTip, Under Cursor:`nahk_id: %id%`nahk_class: %class%`nTitle:%title%`nControl: %control%`n`nWindow Control List:`n%ControlList%

So the controls of my google chrome beta (36714) are:

  • ViewsTextfieldEdit1
  • Chrome_RenderWidgetHostWND1
  • Chrome_AutocompleteEditView1
  • Chrome_WindowImpl_01
  • Chrome_WindowImpl_02