1

I'm trying to use AutoHotKey to fix the backspace key in Chrome. My first attempt utilized the following solution from this question:

#IfWinActive, ahk_class Chrome_WidgetWin_1
BackSpace::Send, {Left}{Delete}

It works, but it unfortunately also prevents me from selecting text in an input/textarea and removing it all with the backspace key. So I started looking for a way to exclude cases where the cursor is in one of those. I then found this answer, with a script for changing the window title when inside an input/textarea and the following AHK script to make use of that title update:

SetTitleMatchMode, RegEx

#x:: ; normal hotkey
     ; do something
    return

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

#IfWinActive

I've been trying to mix the two together, so that I can rewrite the behaviour of Backspace when in a Chrome window that does not end in [AHK] - Google Chrome, but I have been unsuccessful so far. I feel like I'm really close and just missing something, but I am an absolute novice at AHK and I have no idea where to go from here. After meddling with the above code, and searching online, here's what I have right now:

SetTitleMatchMode 2 ; partial title matches

IfWinActive, ahk_class Chrome_WidgetWin_1 ; identify Chrome
{
    #BackSpace::
    If WinActive("[AHK] - Google Chrome") ; identify when within text element
    {
        return
    }
    else {
        Send, {Left}{Delete}
    }
}

I feel like I should be able to accomplish this with a single IfWinActive statement to include Chrome and exclude the [AHK] stuff, but the docs don't give any examples for adding the other parameters past the first one, and my own attempts have been futile. Any help with this would be greatly appreciated. Also, if there's a way to similarly exclude the address bar, that would be stellar.

DiMono
  • 133
  • 6

1 Answers1

1
#ifWinActive, ahk_class Chrome_WidgetWin_1

bs::
    clipboard =  ; Clear clipboard
    send ^c  ; Copy any selected text
    if clipboard =  ; If nothing is selected
        send {left}
    send {del}
return

#ifWinActive
Adam Lui
  • 366
  • 3
  • 11
  • That's neat, but unfortunately the problem I'm addressing also happens in a text field under certain conditions. What I need to identify is if the cursor is in a text field *and* text is selected, in which case I should send {delete} rather than {left}{delete} – DiMono Sep 24 '14 at 17:28
  • I just realized that I don't know what your original reason for the AHK is, so I must ask, is pressing backspace in text fields in Chrome causing the browser to navigate back? – Adam Lui Sep 25 '14 at 02:32
  • It is. Backspace is either always or most of the time (don't remember, AHK at work) navigating back even when I'm trying to delete text I've written. It's especially frustrating when I'm playing something like Town of Salem, where a big part of the game is typing, and if I press backspace I'm actually dropped from the game. – DiMono Sep 26 '14 at 03:35
  • Okay I updated the code so that it checks to see if anything is selected first. – Adam Lui Sep 26 '14 at 07:31