I have AutoHotkey set up in a way that is recognises Cmd+Left and Cmd+Right keystrokes as Back/Forward navigation in Chrome.
Problem is that it also recognises those keys being pressed while I am entering text in text boxes. However when entering text, I'd would like those key combinations to carry out different function – jump to beginning/end of the line, similar to Ctrl+Left/Right.
Is there a way to have one mapping working for text boxes and another mapping for everything else in AutoHotkey?
Best Answer
The deepest AutoHotkey (AHK) can detect within Google Chrome, related to web content, is the
Chrome_RenderWidgetHostHWND1
control. This control is the viewport for web content. Ourinput
andtextarea
HTML elements are a little deeper, inside the DOM of the rendered content.So, we need to find a way to communicate through this wall between the HTML elements and AutoHotkey. Luckily, there is a player on the same side as the HTML who can speak AHK's lingo: JavaScript!
How so? To put it simply, JavaScript can perform an action when an element gains or loses focus. One of these actions can be changing the page title. This in turn changes Google Chrome's window title, and AHK can read window titles!
Setup:
Install my InputFocusTitleChange.user.js userscript. (Source)
Now you can use following syntax in AutoHotkey:
Demonstration:
When an HTML
input
ortextarea
element has focus, the userscript appends[AHK]
to the page title, changing Google Chrome's window title.AHK can use this characteristic, if the window's title ends in
"[AHK] - Google Chrome"
or not, to indirectly connect HTMLinput
/textarea
focus with a set of commands.Notes:
From my testing, there are two issues:
If the
input
/textarea
is already focused when the page loads,[AHK]
may not be added to the page title. You have to move focus away from the element and then back to get the title to change.If the
input
/textarea
element is dynamically generated after the page loads (this is also when the script loads), then those elements will not have any effect.