Ctrl+W doesn't work after Chrome browser is focused Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Ctrl+W doesn't work after Chrome browser is focused

30 Sep 2019, 03:41

I'm using this code:

Code: Select all

^g::
	Clipboard := ""
	SendInput, ^c
	ClipWait, 0.5
	if( ErrorLevel )
	{
		return
	}
	Run, % "https www.google.ru search?q=" . UriEncode( trim2( Clipboard ) ),, pid
	WinWait, ahk_pid pid
	WinActivate, ahk_pid pid
It works fine, Chrome window is activated (even without WinWait+WinActivate). But the problem is that Chrome window does not always react to keyboard, such as pressing of Up or Down arrows, or, what's more important, Ctrl+W. Actually it reacts, but only in 50% of cases. Even after scrolling the page, it does not react to Ctrl+W - you need to click on the page, or just anywhere on Chrome window, to make Ctrl+W work. That's why I tried WinWait+WinActivate, but it doesn't change a thing :headwall:

So is there any method for force activating Chrome window - or, I must say, separately Chrome tab, to make Ctrl+W work without clicking on the page itself?
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Ctrl+W doesn't work after Chrome browser is focused  Topic is solved

30 Sep 2019, 11:00

pid is a variable name in your code - to reference its contents with winwait or winActivate, you will have to put it between %s or force an expression.

Code: Select all

WinWait, ahk_pid %pid%
WinActivate, ahk_pid %pid% 
; or:
WinWait, % "ahk_pid " pid
WinActivate, % "ahk_pid " pid
Also, if I see it correctly, your use of run doesn't correspond to the docs. According to run, the process id variable name should be the fourth parameter - but this looks like the third parameter to me:

Code: Select all

Run, % "https www.google.ru search?q=" . UriEncode( trim2( Clipboard ) ),, pid
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: Ctrl+W doesn't work after Chrome browser is focused

01 Oct 2019, 05:44

gregster wrote:
30 Sep 2019, 11:00
pid is a variable name in your code - to reference its contents with winwait or winActivate, you will have to put it between %s or force an expression.
Yes, of course! I should have taken that into account.
gregster wrote:
30 Sep 2019, 11:00
Also, if I see it correctly, your use of run doesn't correspond to the docs.
Obviously, it is :D Thanks alot!

I also removed the WinWait procedure because it sometimes caused the script to hang.
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: Ctrl+W doesn't work after Chrome browser is focused

01 Oct 2019, 05:59

I have tested it, and now Chrome window is activated in 100% of cases. If anyone is interested, here is the full code (in the URL, replace underscores '_' with slashes '/'). This script launches Google search with the text you selected (in Word, Notepad, anywhere, even from the Chrome itself):

Code: Select all

#NoEnv
;#NoTrayIcon
SetBatchLines, -1
#Persistent
SetWorkingDir, %A_ScriptDir%
Menu, Tray, Icon, google.png


UriEncode( uri, enc="UTF-8" )
{

	StrPutVar( uri, var, enc )
	f := A_FormatInteger
	SetFormat, IntegerFast, H

	Loop
	{

		code := NumGet( var, A_Index - 1, "UChar" )

		if( ! code )
		{
			break
		}

		if( code >= 0x30 && code <= 0x39 ; 0-9
			|| Code >= 0x41 && code <= 0x5A ; A-Z
			|| code >= 0x61 && code <= 0x7A ) ; a-z
		{
			res .= Chr( code )
		}
		else
		{
			res .= "%" . SubStr( code + 0x100, -1 )
		}

	}

	SetFormat, IntegerFast, %f%

	return res

}

StrPutVar( str, ByRef var, enc="" )
{

	len := StrPut( str, enc ) * ( enc = "UTF-16" || enc = "CP1200" ? 2 : 1 )
	VarSetCapacity( var, len, 0 )
	return StrPut( str, &var, enc )

}

trim2( str )
{

	return RegExReplace( str, "^[\s ]+|[\s ]+$", "" )

}

clean_spaces( str )
{

	return RegExReplace( RegExReplace( str, " ", " " ), " {2,}", " " )

}




^g::

	Clipboard := ""
	SendInput, ^c
	ClipWait, 5

	if( ErrorLevel )
	{
		return
	}

	;MsgBox, % "https:__www.google.ru_search?q=" . UriEncode( clean_spaces( trim2( Clipboard ) ) )
	Run, % "https:__www.google.ru_search?q=" . UriEncode( clean_spaces( trim2( Clipboard ) ) ),,, pid
	;WinWait, % "ahk_pid ", pid,, 0.5       ; sometimes causes script to hang :(
	WinActivate, % "ahk_pid ", pid
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Ctrl+W doesn't work after Chrome browser is focused

01 Oct 2019, 08:10

I wish more people knew about Chrome extensions. They would save countless ours of tedious coding by using the right tools for the job.

What you wanted is Ctrl+G to copy the selected text and then open a google search for that text. I do something like that as well.

You can set that up in 10 seconds with this Chrome extension:
https://chrome.google.com/webstore/detail/autocontrol-shortcut-mana/lkaihdpfpifdlgoapbfocpmekbokmcfd

Image

The two red arrows show that the shortcut will work globally (i.e. on any program).
And the key part is in the URL you have to open (in red):
https://www.google.ru/search?q=<clipboard>

The sequence of actions read like this:
1. Copy the selected text to the clipboard
2. Open the URL in a new tab
3. Activate that tab
4. Focus the Chrome window (in case it wasn't already focused)
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: Ctrl+W doesn't work after Chrome browser is focused

01 Oct 2019, 09:03

Getfree wrote:
01 Oct 2019, 08:10
I wish more people knew about Chrome extensions. They would save countless ours of tedious coding by using the right tools for the job.
To be honest, I rarely assume that Chrome extensions are able to operate outside the browser :) Thank you, I'll try it!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: OrangeCat and 240 guests