[V2] Tooltip2 to follow mouse with timeout

Post your working scripts, libraries and tools.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

[V2] Tooltip2 to follow mouse with timeout

Post by AHK_user » 22 Mar 2023, 06:28

I have created a function to display the tooltip that follows the mouse and stops after a time.
Strange that I could not find it on the forum for V2, or I missed some forums.

Maybe a nice example for in the Tooltip documentation?

UPDATED:
- Switched to use A_TickCount to determine the TargetTime
- Modified Simplefied to eliminate flickering

Code: Select all

#Requires AutoHotkey v2.0-rc.2
ToolTip2(Text?, TimeSeconds:=5, WhichToolTip?){
	; Shows a Tooltip following the mouse for a specified time
	SetTimer(((Text?,TickCountTarget?,WhichToolTip?)=>( (TickCountTarget<A_TickCount) ? (ToolTip("",,,WhichToolTip?), SetTimer(,0)) : (ToolTip(Text?,,,WhichToolTip?)))).Bind(Text?,A_TickCount+TimeSeconds*1000,WhichToolTip?), 50)
}

Here is the identical script, but simplified, as this is more readable to understand for educational purposes.
Fat arrow functions are efficient, but not always readable.
Maybe this version is more suited as an example in the documentation. :think:

Code: Select all

#Requires AutoHotkey v2.0-rc.2
ToolTip2_Simplefied(Text?, TimeSeconds:=5, WhichToolTip?){
	SetTimer(ToolTipSub.Bind(Text?, A_TickCount+TimeSeconds*1000, WhichToolTip?), 50)
	X_Mouse_Prev := ""
	Y_Mouse_Prev := ""

	ToolTipSub(Text?, TimeTarget?, WhichToolTip?){

		if (A_TickCount>TimeTarget){
			ToolTip("",,,WhichToolTip?)
			SetTimer(,0)
		} else {
			MouseGetPos(&X_Mouse, &Y_Mouse)
			if (X_Mouse_Prev!=X_Mouse or Y_Mouse_Prev!=Y_Mouse){
				ToolTip(Text?,,,WhichToolTip?)
				X_Mouse_Prev := X_Mouse
				Y_Mouse_Prev := Y_Mouse
			}
		}
	}
}
Question: Does somebody has a better/shorter version? I would love to imp
Last edited by AHK_user on 02 Apr 2023, 02:56, edited 8 times in total.

Tomshi
Posts: 6
Joined: 06 Sep 2022, 20:58
Contact:

Re: [V2] Tooltip2 to follow mouse with timeout

Post by Tomshi » 23 Mar 2023, 04:22

Simply calling the tooltip function constantly is a bad idea, if the user's mouse coord hasn't changed you can end up with flickering which isn't ideal.

User avatar
NPerovic
Posts: 35
Joined: 31 Dec 2022, 01:25
Contact:

Re: [V2] Tooltip2 to follow mouse with timeout

Post by NPerovic » 23 Mar 2023, 05:10

I think the only difference is that I use A_TickCount rather than DateAdd().

Code: Select all

TT2(Text?, TimeOut := 5, WhichToolTip?) => (
    tick := A_TickCount, SetTimer(Update, 100),
    Update() => (
        (A_TickCount-tick < TimeOut*1000) 
        ? ToolTip(Text?, , , WhichToolTip?)
        : (SetTimer(, 0), ToolTip(, , , WhichToolTip?))
    )
)
Collection of My AHK v2 Scripts
⭐ DarkThemeListView.ahk - Dark Theme ListView Control
⭐ KeyboardLayout.ahk - Switching between Keyboard Layouts
⭐ Dark_WindowSpy.ahk - Dark Theme Window Spy
⭐ SystemThemeAwareToolTip.ahk - Tooltip with System Theme Awareness
⭐ MouseHook.ahk - Tool for Intercepting Mouse Events
⭐ ToolTipEx.ahk - Extension for Custom Tooltips

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Tooltip2 to follow mouse with timeout

Post by AHK_user » 24 Mar 2023, 10:40

NPerovic wrote:
23 Mar 2023, 05:10
I think the only difference is that I use A_TickCount rather than DateAdd().

Code: Select all

TT2(Text?, TimeOut := 5, WhichToolTip?) => (
    tick := A_TickCount, SetTimer(Update, 100),
    Update() => (
        (A_TickCount-tick < TimeOut*1000) 
        ? ToolTip(Text?, , , WhichToolTip?)
        : (SetTimer(, 0), ToolTip(, , , WhichToolTip?))
    )
)
That is indeed shorter, you also used the unset option in the function variables, I did not used that before, thanks! :D
I have updated the code.

aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: [V2] Tooltip2 to follow mouse with timeout

Post by aliztori » 25 Mar 2023, 07:29

AHK_user wrote:
22 Mar 2023, 06:28
I have created a function to display the tooltip that follows the mouse and stops after a time.
Strange that I could not find it on the forum for V2, or I missed some forums.

Maybe a nice example for in the Tooltip documentation?

UPDATED:
- Switched to use A_TickCount to determine the TargetTime

Code: Select all

#Requires AutoHotkey v2.0-rc.2
ToolTip2(Text?, TimeSeconds:=5, WhichToolTip?){
	; Shows a Tooltip following the mouse for a specified time
	SetTimer(((Text?,TickCountTarget?,WhichToolTip?)=>( (TickCountTarget<A_TickCount) ? (ToolTip("",,,WhichToolTip?), SetTimer(,0)) : (ToolTip(Text?,,,WhichToolTip?)))).Bind(Text?,A_TickCount+TimeSeconds*1000,WhichToolTip?), 50)
}

Here is the identical script, but simplified, as this is more readable to understand for educational purposes.
Fat arrow functions are efficient, but not always readable.
Maybe this version is more suited as an example in the documentation. :think:

Code: Select all

#Requires AutoHotkey v2.0-rc.2
ToolTip2_Simplefied(Text?, TimeSeconds:=5, WhichToolTip?){
	SetTimer(ToolTipSub.Bind(Text?, A_TickCount+TimeSeconds*1000, WhichToolTip?), 50)

	ToolTipSub(Text?, TimeTarget?, WhichToolTip?){
		if (A_TickCount>TimeTarget){
			ToolTip("",,,WhichToolTip?)
			SetTimer(,0)
		} else {
			ToolTip(Text?,,,WhichToolTip?)
		}
	}
}
Question: Does somebody has a better/shorter version? I would love to imp
I have been making this type of tool for a long time, but I don't know why it doesn't work
Of course, with this ability, we can call the tooltip in this way

Code: Select all

ToolTip2()
tooltip and its timer should be turned off completely

Because of this problem that exists and I don't know what it is

Code: Select all

^g::{

    static toggle := false
    SetTimer(ToolTip.Bind("text"), toggle := !toggle)
  
}
Because every function except the tooltip worked like a click

User avatar
NPerovic
Posts: 35
Joined: 31 Dec 2022, 01:25
Contact:

Re: [V2] Tooltip2 to follow mouse with timeout

Post by NPerovic » 31 Mar 2023, 12:53

Tomshi wrote:
23 Mar 2023, 04:22
Simply calling the tooltip function constantly is a bad idea, if the user's mouse coord hasn't changed you can end up with flickering which isn't ideal.
Good point, so I updated the function.

Code: Select all

ToolTipX(Text?, TimeOut := 5, EnableToggle := false, WhichToolTip?, *)
@param {any} Text
If omitted, the existing tooltip and its timer (if any) will be closed. Otherwise, this parameter is the text to display in the tooltip.

@param {number} TimeOut
The tooltip will be hidden after the set number of seconds.
If you do not want the tooltip to be hidden automatically after a timeout, set this parameter to 0.

@param {number} enableToggle
Allow to show/hide a tooltip with the same hotkey.
If the tooltip has been automatically hidden due to a timeout, the toggle status will switch to off as well.

To update the content of a tooltip that is currently shown on the screen, set this parameter to false (or 0).
You can still manually turn off the tooltip before the timeout by omitting all parameters like this: ToolTipX()

@param {number} WhichToolTip
Omit this parameter if you don't need multiple tooltips to appear simultaneously.
Otherwise, this is a number between 1 and 20 to indicate which tooltip window to operate upon.
If unspecified, that number is 1 (the first).

@returns {void}

Code: Select all

/**
 * Creates a Pop-up Tooltip window that follows the mouse movement.
 * @param {any} Text
 * If omitted, the existing tooltip (if any) will be hidden. Otherwise,
 * this parameter is the text to display in the tooltip.
 * @param {number} TimeOut
 * The tooltip will be hidden after the set number of seconds.
 * If you do not want the tooltip to be hidden automatically after a timeout, set this parameter to 0.
 * @param {number} enableToggle
 * Allow to show/hide a tooltip with the same hotkey.
 * If the tooltip has been automatically hidden due to a timeout, the toggle status will switch to off as well.
 * @param {number} WhichToolTip
 * Omit this parameter if you don't need multiple tooltips to appear simultaneously. 
 * Otherwise, this is a number between 1 and 20 to indicate which tooltip window to operate upon.
 * If unspecified, that number is 1 (the first).
 * @returns {void}
 */
ToolTipX(Text?, TimeOut := 5, EnableToggle := false, WhichToolTip?, *) 
{
	Static Toggle := false

	if !IsSet(Text) || !(Toggle := !Toggle) 
	{ 
		SetTimer(Updating, 0),
		ToolTip(,,, WhichToolTip?)

		if EnableToggle || !IsSet(Text)
			return
	}
	ttw := ToolTip(Text?, , , WhichToolTip?),

	anchorPt    := Buffer(8),     winRect := Buffer(16),
	excludeRect := Buffer(16),    outRect := Buffer(16),
	winSize     := winRect.ptr+8,
	
	flags       := (VerCompare(A_OSVersion, "6.2") < 0 ? 0 : 0x10000),
	
	tick := A_TickCount,
	SetTimer(Updating, -1)

	Updating()
	{
		if (A_TickCount-tick > TimeOut*1000) || (EnableToggle && !Toggle) 
		{
			ToolTip(,,,WhichToolTip?),
			Toggle := 0
			return
		}
		SetWinDelay(-1),
		CoordMode("Mouse", "Screen"),	
		MouseGetPos(&x, &y),
		
		NumPut("int", x+16, "int", y+16, anchorPt),
		NumPut("int", x-3, "int", y-3, "int", x+3, "int", y+3, excludeRect),

		DllCall("GetClientRect", "ptr", ttw, "ptr", winRect),
		DllCall("CalculatePopupWindowPosition", "ptr", anchorPt, "ptr", winSize, "uint", flags, "ptr", excludeRect, "ptr", outRect)

		Try WinMove(NumGet(outRect, 0, 'int'), NumGet(outRect, 4, 'int'),,, ttw)
		Catch 
			Return
		Else SetTimer(, -1)
	}
}
Inspired by lexikos's original post here: https://www.autohotkey.com/boards/viewtopic.php?p=459758&sid=0df64ba643e477797d43e1db32dd6b69#p459758
Collection of My AHK v2 Scripts
⭐ DarkThemeListView.ahk - Dark Theme ListView Control
⭐ KeyboardLayout.ahk - Switching between Keyboard Layouts
⭐ Dark_WindowSpy.ahk - Dark Theme Window Spy
⭐ SystemThemeAwareToolTip.ahk - Tooltip with System Theme Awareness
⭐ MouseHook.ahk - Tool for Intercepting Mouse Events
⭐ ToolTipEx.ahk - Extension for Custom Tooltips

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Tooltip2 to follow mouse with timeout

Post by AHK_user » 02 Apr 2023, 11:28

@NPerovic This is indeed way better and seems to run smootly.

Cool to see that V2 now returns the hwnd of the tooltip.

I also tried a to create a version that just offsets the tooltip from the mouse to see the difference .
Your version reacts better when moving the mouse to the right side of the screen, quite nice.

Thanks a lot! :bravo:

This is one for my standard library :D

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: [V2] Tooltip2 to follow mouse with timeout

Post by Spitzi » 10 Jan 2024, 16:29

@AHK_user and @NPerovic

Thank you both for this beautiful function. Works nicely and found it's way to my standard library, too.

Before finding this thread, i have created a function that does not follow the mouse cursor, but just shows the tooltip for a given time, without blocking execution of the code that follows the function call.

Code: Select all

	; shows a tooltip and clears it after a given amount of time
	; does not stop execution for that time
	; - TTtext: text to show
	; - timeOut: time to show the tooltip
	; - TTnumber: number between 1 and 20 to select, which tooltip to use
	; - posX, posY: where to show
	ShowToolTipForTime(TTtext, timeOut:=3000, TTnumber:=2, posX:="", posY:="") {
		if IsInteger(posX) && isInteger(posY)
			ToolTip(TTtext, posX, posY, TTnumber)
		else
			ToolTip(TTtext, , , TTnumber)
		SetTimer () => ToolTip("", , , TTnumber), -timeOut
	}
Maybe it comes in handy for somebody.

Greets Spitzi

reddyshyam
Posts: 37
Joined: 24 Jul 2023, 04:34

Re: [V2] Tooltip2 to follow mouse with timeout

Post by reddyshyam » 30 Jan 2024, 10:29

Thanks buddy for the useful script! Serves my purpose. 👏

pgeugene
Posts: 29
Joined: 27 Jun 2019, 04:36

Re: [V2] Tooltip2 to follow mouse with timeout

Post by pgeugene » 17 Feb 2024, 05:30

Spitzi wrote:
10 Jan 2024, 16:29
@AHK_user and @NPerovic

Thank you both for this beautiful function. Works nicely and found it's way to my standard library, too.

Before finding this thread, i have created a function that does not follow the mouse cursor, but just shows the tooltip for a given time, without blocking execution of the code that follows the function call.

Code: Select all

	; shows a tooltip and clears it after a given amount of time
	; does not stop execution for that time
	; - TTtext: text to show
	; - timeOut: time to show the tooltip
	; - TTnumber: number between 1 and 20 to select, which tooltip to use
	; - posX, posY: where to show
	ShowToolTipForTime(TTtext, timeOut:=3000, TTnumber:=2, posX:="", posY:="") {
		if IsInteger(posX) && isInteger(posY)
			ToolTip(TTtext, posX, posY, TTnumber)
		else
			ToolTip(TTtext, , , TTnumber)
		SetTimer () => ToolTip("", , , TTnumber), -timeOut
	}
Maybe it comes in handy for somebody.

Greets Spitzi
I am not very good about AHK
Can you provide an example about how to use your tooltip function ?

reddyshyam
Posts: 37
Joined: 24 Jul 2023, 04:34

Re: [V2] Tooltip2 to follow mouse with timeout

Post by reddyshyam » 17 Feb 2024, 07:40

pgeugene wrote:
17 Feb 2024, 05:30
I am not very good about AHK
Can you provide an example about how to use your tooltip function ?
Hi @pgeugene,

open a text file, copy the below code, save it as test.ahk and run it

Code: Select all

	#Requires AutoHotkey >=v2.0
	
	ShowToolTipForTime(TTtext, timeOut:=3000, TTnumber:=2, posX:="", posY:="") {
		if IsInteger(posX) && isInteger(posY)
			ToolTip(TTtext, posX, posY, TTnumber)
		else
			ToolTip(TTtext, , , TTnumber)
		SetTimer () => ToolTip("", , , TTnumber), -timeOut
	}
	
	ShowToolTipForTime("hi",,2,,)


It will display tooltip hi for 2 seconds

Below is the output.
image.png
image.png (18.29 KiB) Viewed 447 times

pgeugene
Posts: 29
Joined: 27 Jun 2019, 04:36

Re: [V2] Tooltip2 to follow mouse with timeout

Post by pgeugene » 21 Feb 2024, 01:23

reddyshyam wrote:
17 Feb 2024, 07:40
pgeugene wrote:
17 Feb 2024, 05:30
I am not very good about AHK
Can you provide an example about how to use your tooltip function ?
Hi @pgeugene,

open a text file, copy the below code, save it as test.ahk and run it

Code: Select all

	#Requires AutoHotkey >=v2.0
	
	ShowToolTipForTime(TTtext, timeOut:=3000, TTnumber:=2, posX:="", posY:="") {
		if IsInteger(posX) && isInteger(posY)
			ToolTip(TTtext, posX, posY, TTnumber)
		else
			ToolTip(TTtext, , , TTnumber)
		SetTimer () => ToolTip("", , , TTnumber), -timeOut
	}
	
	ShowToolTipForTime("hi",,2,,)


It will display tooltip hi for 2 seconds

Below is the output.

image.png
Hi
I tried to increase the time to 9s But Tooltip seem to popup for 2s and gone.

Code: Select all

ShowToolTipForTime("hi",,9,,)

reddyshyam
Posts: 37
Joined: 24 Jul 2023, 04:34

Re: [V2] Tooltip2 to follow mouse with timeout

Post by reddyshyam » 21 Feb 2024, 02:09

pgeugene wrote:
21 Feb 2024, 01:23
Hi
I tried to increase the time to 9s But Tooltip seem to popup for 2s and gone.

Code: Select all

ShowToolTipForTime("hi",,9,,)
Hi @pgeugene

My bad. TTnumber is WhichToolTip, explanation below. We need to set timeOut. I have simplified the method to take seconds instead of 1000s

Type: Integer
If omitted, it defaults to 1 (the first tooltip). Otherwise, specify a number between 1 and 20 to indicate which tooltip to operate upon when using multiple tooltips simultaneously.

Code: Select all

	
	#Requires AutoHotkey >=v2.0
	
	ShowToolTipForTime(TTtext, timeOut:=3, TTnumber:=2, posX:="", posY:="") {
		if IsInteger(posX) && isInteger(posY)
			ToolTip(TTtext, posX, posY, TTnumber)
		else
			ToolTip(TTtext, , , TTnumber)
			
		SetTimer () => ToolTip("", , , TTnumber), -timeOut*1000
	}
	
	ShowToolTipForTime("hi",5,,,)

Post Reply

Return to “Scripts and Functions (v2)”