Focus a window under the mouse and send a hotkey Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Focus a window under the mouse and send a hotkey

Post by Elermino » 22 Sep 2023, 12:25

Good morning everyone!
When pressing "Ctrl + WheelUp/Down" with the pointer under Chrome I would like to focus the Chrome window once and then send the ^+tab hotkey as shown in my code:

Code: Select all

Ctrl & WheelUp::
MouseGetPos,,, WinUMID
WinGetClass, class, ahk_id %WinUMID%

if (InStr(class,"Chrome_WidgetWin_1")) {
send, {Ctrl down}{Shift down}{tab}
send, {Ctrl up}{Shift up}
}

if not (InStr(class,"Chrome_WidgetWin_1")) {
	IfWinNotActive, ahk_class Chrome_WidgetWin_1
	WinActivate, ahk_class Chrome_WidgetWin_1
}
return

Ctrl & WheelDown::
MouseGetPos,,, WinUMID
WinGetClass, class, ahk_id %WinUMID%

if (InStr(class,"Chrome_WidgetWin_1")) {
send, {Ctrl down}{tab}
send, {Ctrl up}
}

if not (InStr(class,"Chrome_WidgetWin_1")) {
	IfWinNotActive, ahk_class Chrome_WidgetWin_1
	WinActivate, ahk_class Chrome_WidgetWin_1
}
return
I wouldn't want this window to focus every time I execute "Ctrl + WheelUp/Down" because it slows down the tab switching, since I execute the ^+tab code many times while moving the mouse wheel in one direction ↑↓.
Thanks!

GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Focus a window under the mouse and send a hotkey  Topic is solved

Post by GEV » 23 Sep 2023, 05:09

Code: Select all

#If MouseIsOver("ahk_class Chrome_WidgetWin_1")

	^WheelUp:: 
		If !WinActive("ahk_class Chrome_WidgetWin_1")
			WinActivate
		Send, ^{Tab}
	return
	
	^WheelDown:: 
		If !WinActive("ahk_class Chrome_WidgetWin_1")
			WinActivate
		Send, ^+{Tab}
	return

#If

; https://www.autohotkey.com/docs/v1/lib/_If.htm#ExVolume
MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}

Post Reply

Return to “Ask for Help (v1)”