How to detect a click on a specific window?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

How to detect a click on a specific window?

27 Sep 2017, 01:29

What code can I use that will detect if the mouse is clicked on a certain window? Something like...

Code: Select all

IfClickOnWindow, ahk_exe scummvm.exe ; I know that "IfClickOnWindow doesn't exist -- it's just to illustrate my thought process
{
	MsgBox You clicked on the window
}
else
{
	MsgBox You didn't click on the window
}
return
The only caveat is that I would like to be able to detect only the "active area" of the window, in other words, excluding the title bar at the top. Is this possible?

I'm still interested to see both solutions (one wherein you can click anywhere on the window, and one where the title bar is excluded).

Also, I am aware of IfWinActive. But that's not good enough. Because sometimes the window is active, and I still need to know if a click occurs on it.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to detect a click on a specific window?

27 Sep 2017, 02:40

Maybe something like this,

Code: Select all

esc::exitapp

lbutton::MsgBox You didn't click on the window
;#if ClickedOnWindow("ahk_exe scummvm.exe")
#if ClickedOnWindow("ahk_class Notepad")
lbutton::MsgBox You clicked on the window


ClickedOnWindow(wintitle) {
	local wum
	mousegetpos,,,wum
	if winExist(wintitle) == wum
		return 1
	return 0
}
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: How to detect a click on a specific window?

03 Oct 2017, 23:11

Ok, Here's what I've got so far:

Code: Select all

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Sleep, 2000
WinActivate, ahk_class Shell_TrayWnd
Lock = 0

While ScummVM != 0
{
	Process, Exist, scummvm.exe
	ScummVM = %ErrorLevel%
	if(GetKeyState("ctrl", "P") && GetKeyState("m", "P"))
	{
		Lock = 0
	}
	gosub, Loop
}
ExitApp

Loop:
{
	#if ClickedOnWindow("ahk_exe scummvm.exe") && Lock = 0
	{
		~lbutton::
			WinWaitActive, ahk_exe scummvm.exe
			Send {Ctrl down}
			Sleep, 10
			Send m
			Sleep, 10
			Send {Ctrl up}
			Lock = 1
			return
	}
	return
}

ClickedOnWindow(wintitle)
{
	local wum
	mousegetpos,,,wum
	if winExist(wintitle) == wum
		return 1
		return 0
}

return
So, this code works and does almost exactly what I want it to, however, there is a problem. The script makes two assumptions:

1. Right after the scummvm game launches, the user will first click on the window, thus locking the cursor to the window
2. The user will unlock the cursor with ctrl+m and lock by clicking on the window

If the user violates either of these assumptions, then the script gets throw off and clicking starts to unlock the cursor instead. For example, if the user decides to first press ctrl+m or if the user unlocks by pressing ctrl+m, then the script will behave incorrectly. How can I fix this?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to detect a click on a specific window?

04 Oct 2017, 01:48

You need to look in the documentation on #if and hotkeys. I don't know if this is what you want, but it is a cleaned up version of your last post,

Code: Select all

;	https://autohotkey.com/boards/viewtopic.php?f=5&t=37561
;	How to detect a click on a specific window? - AutoHotkey Community - Opera

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Sleep, 2000
WinActivate, ahk_class Shell_TrayWnd
Lock := 0

esc::exitApp
#if processExist("scummvm.exe")
~^m::Lock := 0

#if ClickedOnWindow("ahk_exe scummvm.exe") && Lock = 0
~lbutton::
	WinWaitActive, ahk_exe scummvm.exe
	Send {Ctrl down}
	Sleep, 10
	Send m
	Sleep, 10
	Send {Ctrl up}
	Lock := 1
return

processExist(name){
	Process, Exist, % name
	return ErrorLevel
}

ClickedOnWindow(wintitle)
{
	local wum
	mousegetpos,,,wum
	if winExist(wintitle) == wum
		return 1
	return 0
}
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: How to detect a click on a specific window?

04 Oct 2017, 02:14

Helgef wrote:You need to look in the documentation on #if and hotkeys. I don't know if this is what you want, but it is a cleaned up version of your last post,

Code: Select all

;	https://autohotkey.com/boards/viewtopic.php?f=5&t=37561
;	How to detect a click on a specific window? - AutoHotkey Community - Opera

#NoTrayIcon
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Sleep, 2000
WinActivate, ahk_class Shell_TrayWnd
Lock := 0

esc::exitApp
#if processExist("scummvm.exe")
~^m::Lock := 0

#if ClickedOnWindow("ahk_exe scummvm.exe") && Lock = 0
~lbutton::
	WinWaitActive, ahk_exe scummvm.exe
	Send {Ctrl down}
	Sleep, 10
	Send m
	Sleep, 10
	Send {Ctrl up}
	Lock := 1
return

processExist(name){
	Process, Exist, % name
	return ErrorLevel
}

ClickedOnWindow(wintitle)
{
	local wum
	mousegetpos,,,wum
	if winExist(wintitle) == wum
		return 1
	return 0
}
Hey, thanks for cleaning that up for me!

This looks better; except, I want the script to automatically terminate itself when scummvm.exe is terminated.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to detect a click on a specific window?

04 Oct 2017, 02:51

You can use winWaitClose. Also, I'd change #if ClickedOnWindow("ahk_exe scummvm.exe") && Lock = 0 to #if Lock = 0 && ClickedOnWindow("ahk_exe scummvm.exe"), see Short-circuit Boolean Evaluation. :wave:
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: How to detect a click on a specific window?

04 Oct 2017, 03:16

Helgef wrote:You can use winWaitClose. Also, I'd change #if ClickedOnWindow("ahk_exe scummvm.exe") && Lock = 0 to #if Lock = 0 && ClickedOnWindow("ahk_exe scummvm.exe"), see Short-circuit Boolean Evaluation. :wave:
Forgive my ignorance, but how would I use winwaitclose? Where should it be placed in the code?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to detect a click on a specific window?

04 Oct 2017, 03:40

I suggest you read about the auto-execute section. When understood, you can easily fix this code to demonstrate how you can use winWaitClose,

Code: Select all

#if lock = 0 && WinActive("ahk_class Notepad")
F1::Msgbox Notepad is active

winWaitClose ahk_class Notepad
lock := 0
exitapp

esc::exitapp
Also, you should read the docs on #if, to see how you can make the esc::exitapp hotkey always work.
lukeman3000
Posts: 23
Joined: 20 Aug 2017, 00:52

Re: How to detect a click on a specific window?

07 Oct 2017, 20:07

Helgef wrote:I suggest you read about the auto-execute section. When understood, you can easily fix this code to demonstrate how you can use winWaitClose,

Code: Select all

#if lock = 0 && WinActive("ahk_class Notepad")
F1::Msgbox Notepad is active

winWaitClose ahk_class Notepad
lock := 0
exitapp

esc::exitapp
Also, you should read the docs on #if, to see how you can make the esc::exitapp hotkey always work.
I read through that section, more than once. I also read the other thing you suggested, about #if. So, I got esc working at all times. But as far as the first problem, I can't quite get it. The code gets stuck on the F1:: hotkey despite being underneath #if.. I don't understand. I've tried multiple things but haven't got it yet.

Furthermore, lock will never equal 0 until after notepad closes, at which point the application terminates. So I could just move lock := 0 to the top of the script, but that feels like cheating. Still, even if I did that, the script still gets stuck on the F1 hotkey. Can you point me in the right direction?

Code: Select all

#If lock = 0 && WinActive("ahk_class Notepad")
F1::Msgbox Notepad is active

winWaitClose ahk_class Notepad
lock :=0
exitapp

#If
esc::exitapp
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to detect a click on a specific window?

08 Oct 2017, 05:32

Hello.
So I could just move lock := 0 to the top of the script
That was the idea :thumbup:
The code gets stuck on the F1:: hotkey
That is a one-line hotkey, see hotkeys, near the top.
You should put lock:=0 at the top of the script, and follow it with the winWait. Then the hotkey works only when notepad is active and lock has been set to false (0). The script closes when notepad terminates. Example,

Code: Select all

; Start auto-execute section
lock := 0						; Set lock := 0 for context sensitive hotkey. Equivalent, lock := false
winWaitClose ahk_class Notepad	; Wait for Notepad to close
exitapp							; Terminate script

return 							; End auto-execute section. In this case, the return is not needed since the script will exit.
								; Also, the auto-execute section ends on the first hotkey:: encounter.

; Hotkeys always active:
F2::lock:=!lock 				; Toggle the lock. This means that if lock is 0, it becomes 1, if it is 1, it becomes 0.
esc::exitapp					; Exit the script

; Context sensitive hotkeys.
#if lock = 0 && WinActive("ahk_class Notepad")
F1::Msgbox Notepad is active

#if 							; End context sensitive

; Hotkeys defined after a blank #if is also always active, eg,

^esc::exitapp					; Exit the script

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Frogrammer, Google [Bot] and 271 guests