Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
This is going to be tricky because AHK can't detect individual elements within the web content, but I have some tricks up my sleeve for making this work without much work. ;) –  iglvzx Jun 27 '12 at 5:31
    
What are your tricks, @iglvzx? –  Art Jun 28 '12 at 1:01

1 Answer 1

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. Our input and textarea 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:

  1. Install my InputFocusTitleChange.user.js userscript. (Source)

  2. Now you can use following syntax in AutoHotkey:

    SetTitleMatchMode, RegEx
    
    #x:: ; normal hotkey
         ; do something
        return
    
    #IfWinActive, \[AHK\] - Google Chrome$
        #x:: ; input/textarea focus hotkey
             ; do something
            return
    
    #IfWinActive
    

Demonstration:

When an HTML input or textarea element has focus, the userscript appends [AHK] to the page title, changing Google Chrome's window title.

window title google chrome

AHK can use this characteristic, if the window's title ends in "[AHK] - Google Chrome" or not, to indirectly connect HTML input/textarea focus with a set of commands.


Notes:

From my testing, there are two issues:

  1. 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.

  2. 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.

I will continue to work on this solution. If you have any suggestions or know of a way to counter these JavaScript issues, please feel free to let me know and/or contribute. The source is posted above and hosted on Github.

share|improve this answer
    
Wow this is very detailed! I'll give it a shot soon! Thanks! –  Art Jun 29 '12 at 2:15
    
So how would I go about using this to insert the following: ` #IfWinActive, ahk_class Chrome_WidgetWin_1 BackSpace::Send, {Left}{Delete}` ...when in Chrome but not in a textarea/input? –  DiMono Aug 18 at 3:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.