How to find "permanent" Unique ID / HWND of a window? or anything that is unique and permanent?

Get help with using AutoHotkey and its commands and hotkeys
DeepMind
Posts: 271
Joined: 19 Jul 2016, 14:47

How to find "permanent" Unique ID / HWND of a window? or anything that is unique and permanent?

25 Jul 2016, 20:21

"A window's ID number is valid "only during its lifetime". In other words, if an application restarts, all of its windows will get new ID numbers." that was in this link.
I want to use window's "Unique ID" in "#IfWinActive" to define hotkey in windows that have the same "ahk_class" and i can't use "ahk_exe" because i want to use hotkey in a couple window of the same program so ahk_exe isn't helpful.
So what's the use of HWND if it's gonna change with closing a window?!!! I need something that doesn't get expired. can somebody plz help me out here, tnx :)
Last edited by DeepMind on 26 Jul 2016, 01:07, edited 3 times in total.
hunter99
Posts: 128
Joined: 20 Jan 2014, 17:57

Re: Could someone help me with this sentence that was in a document?

25 Jul 2016, 20:38

And the answer is --- example 1 of the link you posted.
Suggest you try some of the examples given on the the help pages you have looked at in your many posts.
hunter99
User avatar
boiler
Posts: 5913
Joined: 21 Dec 2014, 02:44

Re: Could someone help me with this sentence that was in a document?

25 Jul 2016, 21:14

If you want to use the hotkeys in multiple windows of the same program, then ahk_class and ahk_exe seem like they are what you want. Do you mean you want the hotkey to work only in a specific window of a program that has multiple windows?

The hwnd is useful because once you have identified it, you know it is always going to refer to that specific window, no matter how many other windows there may be with the same title, class, or process. Even if you might not be able to think of a use for it, there are many where that is exactly what is needed.
DeepMind
Posts: 271
Joined: 19 Jul 2016, 14:47

Re: Could someone help me with this sentence that was in a document?

25 Jul 2016, 21:59

boiler wrote:If you want to use the hotkeys in multiple windows of the same program, then ahk_class and ahk_exe seem like they are what you want. Do you mean you want the hotkey to work only in a specific window of a program that has multiple windows?

The hwnd is useful because once you have identified it, you know it is always going to refer to that specific window, no matter how many other windows there may be with the same title, class, or process. Even if you might not be able to think of a use for it, there are many where that is exactly what is needed.
how exactly it's useful if it's gonna change? look, for example i found out Unique_ID of a window with this code:

Code: Select all

WinGet, active_id, ID, A
WinMaximize, ahk_id %active_id%
MsgBox, The active window's ID is "%active_id%".
and then I used it in this code:

Code: Select all

#IfWinActive, ahk_id 0x2054f1
f::Sendinput {Enter}
, and it bringed up the window with a shortcut for the first time but next time it will change so how exactly this is useful? (so didn't work for me) am i missing something obvious here?
lexikos
Posts: 7057
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How to find "permanent" Unique ID / HWND of a window? or anything that is unique and permanent?

26 Jul 2016, 01:31

As it says, a window has a lifetime; there's nothing permanent about a window.

A window's ID doesn't change. The program creates a new window that looks and acts the same, but it's a new window. The ID is useful only up until the window is destroyed.
User avatar
boiler
Posts: 5913
Joined: 21 Dec 2014, 02:44

Re: How to find "permanent" Unique ID / HWND of a window? or anything that is unique and permanent?

26 Jul 2016, 04:19

DeepMind, you are thinking about it wrong. You don't hard-code the hwnd of a window into your script. You have the script determine the hwnd itself and store it in a variable, and you use it to identify that particular window as long as it exists.

Here's an example: Your script opens a Notepad window, and right after it opens, it grabs the hwnd of the active Notepad window so it knows which one it is so it can paste some text in it later. You don't want the script to accidentally paste text in another Nnotepad window that may be open, so you use the hwnd to make sure you keep using the window that the script opened.
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: Could someone help me with this sentence that was in a document?

26 Jul 2016, 05:42

DeepMind wrote: how exactly it's useful if it's gonna change?
This code can be used to identify the HWND and make a context sensitive hotkey for a window which shares ahk_exe and Window title with multiple other windows,
and is killed/remade during script run time. For this to work, the window needs to have a unique set of controls.

Code: Select all

;	; 	;	;	;	;	;	;	;	; 	;	;	;	;	;	;	;	; 	;	;	;	;	;	;
; This can be used to identify the HWND and make a context sensitive hotkey a window for which shares ahk_exe and Window title with multiple other windows,
; and is killed/remade during script run time. For this to work, the window needs to have a unique set of controls.
;
; Possible extention: Consider WinText and do Exclude if possible.
; 
; Start script.
; Setup
									; Read please:
executableName:="notepad++.exe"		; YOU NEED TO CHANGE THIS to your program's name.
KeyWait, Space, D					; Activate the window you want to operate on and press space.
WinGet,controlNames,ControlList,A	; Get all control names in this window. THIS LIST CAN BE HARD CODED TO AVOID THIS SETUP STEP.
return 								; End autoexc.
 
; Hotkeys
#If WinActive("ahk_id" . findHWND(controlNames,executableName))			; Context sensitive hotkey.
f::Enter
#If
  
findHWND(controlNames,executableName) 
{
	WinGet, windowIds, List,% "ahk_exe " . executableName 						
	Loop, %windowIds%													; Note: ControlList gives a `n delimited string, and List gives a pseudo array.
	{
		WinGet,tmpList,ControlList,% "ahk_id " . windowIds%A_Index%  	; From doc: "Controls are sorted according to their Z-order, 
		if (tmpList=controlNames)										; which is usually the same order as TAB key navigation if the window supports tabbing" so the order should be well defined.
			return windowIds%A_Index%									; Window found, return its HWND.
	}
	
	return 0 															; Window not found. 
}

DeepMind
Posts: 271
Joined: 19 Jul 2016, 14:47

Re: Could someone help me with this sentence that was in a document?

25 Aug 2016, 12:45

Helgef wrote:
DeepMind wrote: how exactly it's useful if it's gonna change?
This code can be used to identify the HWND and make a context sensitive hotkey for a window which shares ahk_exe and Window title with multiple other windows,
and is killed/remade during script run time. For this to work, the window needs to have a unique set of controls.

Code: Select all

;	; 	;	;	;	;	;	;	;	; 	;	;	;	;	;	;	;	; 	;	;	;	;	;	;
; This can be used to identify the HWND and make a context sensitive hotkey a window for which shares ahk_exe and Window title with multiple other windows,
; and is killed/remade during script run time. For this to work, the window needs to have a unique set of controls.
;
; Possible extention: Consider WinText and do Exclude if possible.
; 
; Start script.
; Setup
									; Read please:
executableName:="notepad++.exe"		; YOU NEED TO CHANGE THIS to your program's name.
KeyWait, Space, D					; Activate the window you want to operate on and press space.
WinGet,controlNames,ControlList,A	; Get all control names in this window. THIS LIST CAN BE HARD CODED TO AVOID THIS SETUP STEP.
return 								; End autoexc.
 
; Hotkeys
#If WinActive("ahk_id" . findHWND(controlNames,executableName))			; Context sensitive hotkey.
f::Enter
#If
  
findHWND(controlNames,executableName) 
{
	WinGet, windowIds, List,% "ahk_exe " . executableName 						
	Loop, %windowIds%													; Note: ControlList gives a `n delimited string, and List gives a pseudo array.
	{
		WinGet,tmpList,ControlList,% "ahk_id " . windowIds%A_Index%  	; From doc: "Controls are sorted according to their Z-order, 
		if (tmpList=controlNames)										; which is usually the same order as TAB key navigation if the window supports tabbing" so the order should be well defined.
			return windowIds%A_Index%									; Window found, return its HWND.
	}
	
	return 0 															; Window not found. 
}

honestly can't remember where I needed this :lol: but thanks for the tips, I'll try that :)
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to find "permanent" Unique ID / HWND of a window? or anything that is unique and permanent?

25 Aug 2016, 14:17

Hehe, you have a lot of things going on it seems. I think this approach would rarely be needed, but I think the script works.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], Epialis, Google [Bot] and 44 guests