SetMatchMode for control

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

SetMatchMode for control

Post by hemsith14_ » 07 Aug 2022, 23:31

Hey, I use this to find if control is in focus:

Code: Select all

ControlFocus(_ThisControl) 
{
	ControlGetFocus, _Focus
	Return  _Focus = _ThisControl ? True : False
}
How can I set title match so even if the name of the control only has part of the variable in it, it will still match an #if statement?

Thanks

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: SetMatchMode for control

Post by boiler » 08 Aug 2022, 00:36

You can InStr() to check if the contents of one variable are contained within another. There is no “SetMatchMode” for things other than window titles.

By the way, this line doesn’t need the ternary operator:

Code: Select all

	Return  _Focus = _ThisControl ? True : False
It returns True or False like this:

Code: Select all

	Return  _Focus = _ThisControl

The way you have it, you’re saying “If True, return True, else return False” rather than just returning the True or False result.

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

Re: SetMatchMode for control

Post by hemsith14_ » 08 Aug 2022, 10:25

boiler wrote:
08 Aug 2022, 00:36
You can InStr() to check if the contents of one variable are contained within another. There is no “SetMatchMode” for things other than window titles.

By the way, this line doesn’t need the ternary operator:

Code: Select all

	Return  _Focus = _ThisControl ? True : False
It returns True or False like this:

Code: Select all

	Return  _Focus = _ThisControl

The way you have it, you’re saying “If True, return True, else return False” rather than just returning the True or False result.
How do I implement InStr into my function?

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: SetMatchMode for control

Post by boiler » 08 Aug 2022, 12:26

This will return True/1 if the string that is passed to the function is contained in the name of the focused control. I would add the A for the WinTitle parameter as I've shown to make sure it's referencing the active window.

Code: Select all

ControlFocus(_ThisControl) 
{
	ControlGetFocus, _Focus, A
	Return InStr(_Focus, _ThisControl)
}

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

Re: SetMatchMode for control

Post by hemsith14_ » 08 Aug 2022, 12:50

Thanks!

Is there an option to do something like: search only for the first 5 characters of string?

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: SetMatchMode for control

Post by boiler » 08 Aug 2022, 16:00

If what you passed is expected to be exactly the first 5 characters, then you could do this:

Code: Select all

Return SubStr(_Focus, 1, 5) = _ThisControl

Post Reply

Return to “Ask for Help (v1)”