If Mouse is over as a hotkey function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

If Mouse is over as a hotkey function

Post by hemsith14_ » 17 Aug 2022, 21:03

How do I get this to work? When mouse is over the coordinates, ^w should work.

Code: Select all

Hotkey, If, % function(12, 211, 321, 432) ; #if function(12, 211, 321, 432) works fine, this doesn't. why?
Hotkey, ^w, hello
Return

	function(pX1, pY1, pX2, pY2)
	{
		If !WinActive(ahk_exe notepad.exe)
		Return 0
		MouseGetPos, X, Y
		Return (pX1<=X) && (X<=pX2) && (pY1<=Y) && (Y<=pY2)
	}

hello:
msgbox hello
return

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: If Mouse is over as a hotkey function

Post by mikeyww » 17 Aug 2022, 21:15

Read documentation about expressions. Use CoordMode if needed. You can display the coordinates, so that you know what they are.

Code: Select all

#SingleInstance Force
SoundBeep, 1500
fn := Func("function").Bind(12, 12, 3200, 3200)
Hotkey, If, % fn
Hotkey, ^w, Hello, On
Hotkey, If
Return

Hello:
MsgBox, 64, Hello, Hello!
Return

function(pX1, pY1, pX2, pY2) {
 If !WinActive("ahk_exe notepad.exe")
  Return False
 MouseGetPos, X, Y
 Return pX1 <= X && X <= pX2 && pY1 <= Y && Y <= pY2
}

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: If Mouse is over as a hotkey function

Post by hemsith14_ » 17 Aug 2022, 21:33

That's the 2nd time you're sending me this but it's not working, maybe you don't understand what I'm looking for here? Or am I missing something?

^w wont send hello where it should, or anywhere
Last edited by hemsith14_ on 17 Aug 2022, 21:34, edited 1 time in total.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: If Mouse is over as a hotkey function

Post by mikeyww » 17 Aug 2022, 21:34

When you tested it in Notepad, what happened? What mouse coordinates were found and displayed when you added a MsgBox line?

image220817-2236-001_cr.png
Screenshot
image220817-2236-001_cr.png (37.53 KiB) Viewed 1968 times

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: If Mouse is over as a hotkey function

Post by hemsith14_ » 17 Aug 2022, 21:36

mikeyww wrote:
17 Aug 2022, 21:34
When you tested it in Notepad, what happened? What mouse coordinates were found?
Look, it's unrelated to the coordinates, it works perfectly fine if I do:
#If function(12, 211, 321, 432)
^w::
do things

I don't understand the AHK docs regarding on how to do it with the hotkey command. It makes sense with if winactive but not here.
The functions reports the mouse coordinates so I can use different hotkeys when hovering upon different places on the screen, I just want to know how to get it to work when I change the hotkeys to "Hotkey" like I showed here. I suppose it's something simple that I'm missing, maybe in the way that it should be written. A sign here or there.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: If Mouse is over as a hotkey function

Post by mikeyww » 17 Aug 2022, 21:38

You ran my exact script with no changes to the code? If you add a MsgBox line to display the mouse coordinates that are found, what coordinates are they?

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: If Mouse is over as a hotkey function

Post by hemsith14_ » 17 Aug 2022, 21:40

mikeyww wrote:
17 Aug 2022, 21:38
You ran my exact script with no changes to the code?
Yes. It doesn't work as expected. Do you understand my need? It's unrelated to the coordinates. I just need to understand how to use a function with the hotkey command that will work like the same function works with #if.
Last edited by hemsith14_ on 17 Aug 2022, 21:41, edited 1 time in total.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: If Mouse is over as a hotkey function

Post by mikeyww » 17 Aug 2022, 21:41

Yes. Is Notepad active before you trigger the hotkey?

What coordinates are found with your MouseGetPos? You can add a MsgBox line after it, to show the coordinates.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: If Mouse is over as a hotkey function

Post by hemsith14_ » 17 Aug 2022, 21:43

^w sends something as long as notepad is opened, regardless of the hovering location.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: If Mouse is over as a hotkey function

Post by mikeyww » 17 Aug 2022, 21:44

So you are not going to report what coordinates are found?

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: If Mouse is over as a hotkey function

Post by hemsith14_ » 17 Aug 2022, 21:44

^w sends something as long as notepad is opened, regardless of the hovering location.

You can send me the code you'd like me to check..

Your question seems to suggest that you don't understand my need here as I just need to understand something about "Hotkey" even a tutorial would do. I just can't find any. And I don't understand the documentation.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: If Mouse is over as a hotkey function

Post by mikeyww » 17 Aug 2022, 21:52

Code: Select all

#SingleInstance Force
SoundBeep, 1500
Run, notepad
WinWaitActive, ahk_exe notepad.exe,, 5
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the window.
MouseMove, 100, 100
fn := Func("function").Bind(12, 12, 3200, 3200) ; Relative to active window
Hotkey, If, % fn
Hotkey, ^w, Hello, On
Hotkey, If
Return

Hello:
MsgBox, 64, Hello, Hello!
Return

function(pX1, pY1, pX2, pY2) {
 WinGet, proc, ProcessName, A
 MouseGetPos, x, y
 ToolTip, Process = %proc%`n`nx = %x%`, y = %y%
 If !WinActive("ahk_exe notepad.exe")
  Return False
 If x between %pX1% and %pX2%
  If y between %pY1% and %pY2%
   Return True
}
Explanation

I adapted the example from the documentation. It defines fn as a function object before the Hotkey, If statement. This enables that statement to work in setting the context for your Hotkey command, which assigns ^w to the Hello subroutine.

The function has been revised to do some debugging. It displays both the active process name and the relative mouse position in a tooltip. If Notepad is not active, the function returns False and ends. Otherwise, if the relative mouse position is within the stated boundaries, it returns True, and the Hello subroutine is executed, displaying the message box.

I tested the script with Notepad. This demo runs Notepad, and then positions the mouse so that the hotkey will work if pressed before moving the mouse. I showed the earlier screenshot demonstrating that the message box is displayed.

WinActive is an AHK function. As such, it uses expressions as parameters. An example of an expression is a quoted string, as shown.

The script is programmed so that if Notepad is not the active window, the hotkey will not be triggered. In addition, if the mouse position relative to the Notepad window is out of the stated bounds, the hotkey will not be triggered.

The #SingleInstance directive closes an old instance of the same script when the script is run again. This prevents multiple instances of the script from remaining running at the same time.
^w sends something as long as notepad is opened, regardless of the hovering location.
The revised script will assist in providing additional information addressed to your comment, because it will display the process name of the active window, as well as the mouse's position relative to that window. Notepad can be visible without being the active window. If it is the active window, then it has the keyboard focus.

Post Reply

Return to “Ask for Help (v1)”