Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

Get help with using AutoHotkey and its commands and hotkeys
BannerStore
Posts: 46
Joined: 06 Sep 2017, 19:29

Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

03 Feb 2020, 15:06

I don't get it guys.

When I use the MouseGetPos to obtain the window ID of the active window that the mouse is currently over and then, when I use the WinActive(A), the two IDs are different!!!

It took me so long to figure this out. I hate AutoHotkey! I couldn't figure one part of my script out for such a long time.

Why are they different. Is one a different type of ID than the other? What is the exact difference in names?!

Which one is the real ID and then how do I obtain the ID of the window that my mouse is hovering over?
Tcharr
Posts: 40
Joined: 25 Jan 2020, 15:38

Re: Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

03 Feb 2020, 15:32

Are you talking about class, exe, or pid? What does Window Spy tell you about the window in question?
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

03 Feb 2020, 15:35

BannerStore wrote:
03 Feb 2020, 15:06
When I use the MouseGetPos to obtain the window ID of the active window that the mouse is currently over and then, when I use the WinActive(A), the two IDs are different!!!
How exactly are you using them? Something Like this?

Code: Select all

Loop
{
	MouseGetPos,,, PosWin
	Tooltip % PosWin " " WinActive( "A" )
}

Escape::ExitApp
It's always helpful to post your code.
BannerStore
Posts: 46
Joined: 06 Sep 2017, 19:29

Re: Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

03 Feb 2020, 18:07

Going through my script carefully, I realized that the ways I get the window IDs are through GetMousePos, WinActive, and WinGet. Below is a quick script I wrote. I'm going to first retrieve the window IDs of 2 windows. This is accomplished within the MouseIsOver function located at the bottom.

First, run the script. Then, hover your mouse over a Chrome page without activating it (Another webpage should be activated). Press F5 to see the values of the ID variables. It should give you the window ID of the active window and the Chrome page. Then activate Chrome and press control F5 and you'll notice the window ID of the active Chrome page changes. The one from getting mouse position is different than the one from getting WinGet. They are always different. Why? Which do you use?

Code: Select all

Global OverWinID, OverTitle, AWindowID, AWindowTitle

#If (MouseIsOver(Chrome_WidgetWin_1")) and (%ActWinTitle%!=%OverTitle%)
{
^F5::
MsgBox Window IDs: OverTitle = %OverTitle%`n`nAWindowTitle = %AWindowTitle%`n`nOverwinID = %OverWinID%`n`nAWindowID = %AWindowID%`n`n
Return
}
#If

MouseIsOver(WinTitle) {
[b]MouseGetPos,,, OverWinID[/b]
[b]AWindowID:=WinActive(A)[/b]
WinGetTitle, AWindowTitle, A
WinGetTitle, OverTitle, ahk_id %OverWinID%
WinGetClass, OverWinClass, ahk_id %OverWinID%
Return WinTitle=OverWinClass
}
Return

Sometimes, the active window is always 0x0 for the active window no matter which window becomes active. Sometimes, it comes up continually blank. When you get the ID via WinGet and MousePos, the IDs will differ. Which is correct?
TLM wrote:
03 Feb 2020, 15:35
BannerStore wrote:
03 Feb 2020, 15:06
When I use the MouseGetPos to obtain the window ID of the active window that the mouse is currently over and then, when I use the WinActive(A), the two IDs are different!!!
How exactly are you using them? Something Like this?

Code: Select all

Loop
{
	MouseGetPos,,, PosWin
	Tooltip % PosWin " " WinActive( "A" )
}

Escape::ExitApp
It's always helpful to post your code.
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

03 Feb 2020, 19:07

@BannerStore try this & see my comments

Code: Select all

Global OverWinID, OverTitle, AWindowID, AWindowTitle

; directive #If statements do not need to be enclosed in curly braces { }
#If ( MouseIsOver( "Chrome_WidgetWin_1" ) and !WinActive( "ahk_class Chrome_WidgetWin_1" ) )
^F5::
MsgBox Window IDs: OverTitle = %OverTitle%`n`nAWindowTitle = %AWindowTitle%`n`nOverwinID = %OverWinID%`n`nAWindowID = %AWindowID%`n`n
#If

Return

MouseIsOver( WinTitle )
{
	MouseGetPos,,, OverWinID
	AWindowID:=WinActive( "A" )  ; strings must be in quotes 
	WinGetTitle, OverTitle, ahk_id %OverWinID%
	WinGetTitle, AWindowTitle, A
	WinGetClass, OverWinClass, ahk_id %OverWinID%
	Return ( WinTitle = OverWinClass )
}
For future reference, conditions with variables should not be enclosed in percents (ActWinTitle!=OverTitle)
BannerStore
Posts: 46
Joined: 06 Sep 2017, 19:29

Re: Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

05 Feb 2020, 22:48

TLM, Thanks for your help!!

I've been trying to fix other parts of my script so I haven't had time to respond to you so, sorry!

Anyway, I'll try to go over what you suggested and if I can fix what is going on.

I just wanted to let you know and everybody else that I appreciate your help. It helps a lot!

By the way, I put { } and returns everywhere because I still don't have a solid grasp of the syntax of AutoHotkey yet. I keep forgetting which function/commands need which. It's A LOT to memorize! I figure if I put it in, it won't hurt and it might fix somethihng.
TLM wrote:
03 Feb 2020, 19:07
@BannerStore try this & see my comments

Code: Select all

Global OverWinID, OverTitle, AWindowID, AWindowTitle

; directive #If statements do not need to be enclosed in curly braces { }
#If ( MouseIsOver( "Chrome_WidgetWin_1" ) and !WinActive( "ahk_class Chrome_WidgetWin_1" ) )
^F5::
MsgBox Window IDs: OverTitle = %OverTitle%`n`nAWindowTitle = %AWindowTitle%`n`nOverwinID = %OverWinID%`n`nAWindowID = %AWindowID%`n`n
#If

Return

MouseIsOver( WinTitle )
{
	MouseGetPos,,, OverWinID
	AWindowID:=WinActive( "A" )  ; strings must be in quotes 
	WinGetTitle, OverTitle, ahk_id %OverWinID%
	WinGetTitle, AWindowTitle, A
	WinGetClass, OverWinClass, ahk_id %OverWinID%
	Return ( WinTitle = OverWinClass )
}
For future reference, conditions with variables should not be enclosed in percents (ActWinTitle!=OverTitle)
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Can windows have 2 different IDs???!? Getmousepos not equal to WinActive

07 Feb 2020, 16:19

BannerStore wrote:
05 Feb 2020, 22:48
I keep forgetting which function/commands need which. It's A LOT to memorize!
No worries, I've been using AHk for years and I still make mistakes.

Code: Select all

#If ( MouseIsOver( "Chrome_WidgetWin_1" ) and !WinActive( "ahk_class Chrome_WidgetWin_1" ) )
^F5::
MsgBox Window IDs: OverTitle = %OverTitle%`n`nAWindowTitle = %AWindowTitle%`n`nOverwinID = %OverWinID%`n`nAWindowID = %AWindowID%`n`n
Return ; move the return here in case you want to add more hotkeys to the above directive.
#If
Keep at it, you'll get better ;)

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], jeongrak2, Lastresort, paulcopeland, vsub, Yakshongas and 126 guests