Sending keys to Internet Explorer

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Sending keys to Internet Explorer

29 Apr 2015, 04:03

I'm trying to assign a key to close a subwindow for all applications that have them. Under Windows 8.1, it works for MS Access, Visual Studio, SQL Server, and Notepad++. But it doesn't work for IE. Strangely, it does work if I insert a MsgBox before the Send command. Is there anything I can insert instead that doesn't bother the user?

Code: Select all

#IfWinActive ahk_class IEFrame
!z::
	; In IE, it only seems to work when there is MsgBox at the top;  I haven't found an alternative to make it work. 
	MsgBox, You're in IE and you pressed !z. This will close the window.
	; Sleep, 100
	Send, ^w
	Return
!u::
	Send, ^w
	Return
#IfWinActive
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Sending keys to Internet Explorer

29 Apr 2015, 05:31

It looks like Internet Explorer looks for the Alt down state to activate the menu, instead of the up event. Sending another {Alt} when you release your hotkey fixes it for me:

Code: Select all

!z::
	Keywait Alt
	Send {Alt}
    Send, ^w
Return
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Sending keys to Internet Explorer

29 Apr 2015, 11:09

Thank you, that was a good idea. The following code seems to work:

Code: Select all

!z::
    Keywait Alt
    Send, {Alt}  ; activate the menu
    Send, f      ; "_File menu" 
	Sleep, 100   ; without this, it often doesn't work.
	Send, c      ; "_Close tab" menu entry
	Return
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Sending keys to Internet Explorer

29 Apr 2015, 11:34

Alas, {Alt}fc and ^w are not the same thing. My code works until there is only one subwindow open, in which case the "Close tab" command is not available anymore, but ^w still is.

Since the code with the Send, {Alt} didn't work, and since I don't understand what it's supposed to do (When entering keys directly, IE responds to ^w only when the menu is _not_ activated, and it appears to be not activated after the Keywait Alt.), I tried removing it as follows, but that didn't work either:

Code: Select all

!z::
    Keywait Alt
    Send, ^w
	Return
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Sending keys to Internet Explorer

29 Apr 2015, 11:52

It appears your IE responds differently to Alt than mine does, so there are a few things you could try:
• LAlt::Return disables the Alt button, however, for me IE still reacted to it although quirky. Hence my observation it reacts to the button state, not events. So probably useless to disable, but it might behave differently for you.
• Instead of sending another {Alt}, try sending an {Escape}: if the menubar is activated, escape probably removes the focus.

With your latest code sample, after you use your hotkey, manually press Alt or Escape and see after which and how many presses menubar focus is removed. Then script for those keys.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Sending keys to Internet Explorer

29 Apr 2015, 14:10

Micromegas wrote:

Code: Select all

!z::
    Keywait Alt
    Send, ^w
	Return
Didn't work for me either.

Try this one:

Code: Select all

!z::WBGet().quit

; http://ahkscript.org/boards/viewtopic.php?f=5&t=4497
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               ;// based on ComObjQuery docs
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
;//     , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}"   ;// IID_IHTMLWindow2
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
   if (ErrorLevel != "FAIL") {
      lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
      if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Sending keys to Internet Explorer

29 Apr 2015, 14:29

Thank you! :clap:
Great idea to use Escape - that made the counting easy: It just needs to be pressed once. The following solves it:

Code: Select all

!z::
    Keywait Alt ; it even needs it when the hotkey has no Alt, (tried with z::)
    Send, {Alt up}
    Send, {Escape}
    Send, ^w
	Return
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Sending keys to Internet Explorer

29 Apr 2015, 14:35

tmplinshi wrote:Try this one
Thank you, too, tmplinshi. I didn't notice your post until after I posted mine, but it may come in handy, too. I noticed that Visual Studio uses GUIDs for the window name, so this may teach me how to deal with that.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot], OrangeCat and 178 guests