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

autohotkeygoogle-chrome

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

Details

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:

^+i::
if WinExist("ahk_class Chrome_WidgetWin_0")
    WinActivate
else
    Run "C:\Users\vleeshue\AppData\Local\Google\Chrome\Application\chrome.exe"
return

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

Background

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

^+m::
If WinExist("foobar2000")
    WinActivate
else
    Run "C:\Program Files (x86)\foobar2000\foobar2000.exe"
return

Best Answer

^+i::
if WinExist("ahk_class Chrome_WindowImpl_0")
  {
  WinActivate
  ControlFocus, Chrome_AutocompleteEditView1
  }
else
  Run "C:\Users\vleeshue\AppData\Local\Google\Chrome\Application\chrome.exe"
return

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:

#Persistent
SetTimer, WatchCursor, 100
return

WatchCursor:
  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%
return

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

  • ViewsTextfieldEdit1
  • Chrome_RenderWidgetHostWND1
  • Chrome_AutocompleteEditView1
  • Chrome_WindowImpl_01
  • Chrome_WindowImpl_02