Opening URL loses focus on browser window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
huaba
Posts: 3
Joined: 19 May 2018, 10:57

Opening URL loses focus on browser window

19 May 2018, 11:32

Hey,
i've got a simple question and maybe there is an elegant solution to it.
I've bound different keybinds to open an URL in my browser. In this example Firefox, or the default browser if i just use:
Run, http://myurlhere.com

It looks like this:
^MButton::
Run, C:\Program Files (x86)\Mozilla Firefox\firefox.exe -url http://myurlhere.com/
return
However some of this websites have e.g. a search box which is selected upon loading the site. A good example would be Google, if you go to Google the search field has the cursor in it.
When i press in my example above CTRL+MiddleMouseButton the browser opens the website in a new tab but also i also lose focus on the website. This means that i have to click on my browser window to start typing. When i press the keybind a second time it works and i have the website in a new tab + focus on the browser window. This then goes back and forth if i press it multiple times.

I don't really understand why this is the case and i tried different solutions with different AHK commands.

Anyone any idea on how to tell my AHK script to focus the window again?

BR
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Opening URL loses focus on browser window

19 May 2018, 13:57

removed my reply, I made a mistake thinking you are using comobj, the .focus() has nothing to do with your problem sorry
Last edited by AHKStudent on 20 May 2018, 07:41, edited 1 time in total.
huaba
Posts: 3
Joined: 19 May 2018, 10:57

Re: Opening URL loses focus on browser window

20 May 2018, 05:05

My code is very simple, i googled your reply and tried different JS commands, but not sure how it really works.
Do you have any example i could use or explain your answer in more detail?
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Opening URL loses focus on browser window

20 May 2018, 07:52

huaba wrote:My code is very simple, i googled your reply and tried different JS commands, but not sure how it really works.
Do you have any example i could use or explain your answer in more detail?
does this for example work? I cannot replicate your issue, for me anytime I run it has the cursor in the search box

Code: Select all

^MButton::
Run, http://www.google.com
return
also try

Code: Select all

^MButton::
Run, C:\Program Files (x86)\Mozilla Firefox\firefox.exe -url http://myurlhere.com/
sleep, 2000
WinMaximize, A 
return

Code: Select all

^MButton::
Run, C:\Program Files (x86)\Mozilla Firefox\firefox.exe -url http://myurlhere.com/
sleep, 2000
CoordMode, Mouse, Screen
MouseMove, A_ScreenWidth/2, A_ScreenHeight/2
Click
return
Just trying to get an idea whats up
Stavencross
Posts: 90
Joined: 24 May 2016, 16:42

Re: Opening URL loses focus on browser window

20 May 2018, 11:18

You're opening the url in the foreground causing it to take focus. Look for a command line switch that opens the Firefox tab in the background.

I did a Google search and found the below, but I'm on mobile and couldn't test.

Code: Select all

 
Run, firefox.exe "http://www.google.com/" &
huaba
Posts: 3
Joined: 19 May 2018, 10:57

Re: Opening URL loses focus on browser window

20 May 2018, 12:17

@Stavencross: That's not what i want, i open the URL intentionally in the foreground.

@AHKStunden: Thanks for testing and the examples, just the last one worked as expected, but has to be started as admin to work.
What i found is that with my Chrome Portable this problem doesn't occur, it doesn't matter how often i press the keybind the focus is always on the search bar in Google.
With Firefox if i press the keybind the first new tab is out of focus, if i press it again the next new tab has focus, then not, then yes, and so on. The main issue is that it doesn't has the focus on the first time, which makes me to click on the browser window.

Fortunately i found this solution:

Code: Select all

^MButton::
Run, C:\Program Files (x86)\Mozilla Firefox\firefox.exe -url http://www.google.com/
winwait ahk_class MozillaWindowClass
WinActivate, ahk_exe firefox.exe
return
The winwait doesn't need delay as the browser is already open and WinActivate works immediatly.

Thanks for your replies, helped me a lot finding the solution myself :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, rc76 and 207 guests