Gui follows mouse and shows information until the user clicks - click not always working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Gui follows mouse and shows information until the user clicks - click not always working

Post by Spitzi » 26 Mar 2023, 08:30

Hello.

I have this code to show information to the user about what he should do next. If the user clicks, the message should dissapear

Code: Select all

ShowInfoGuiUntilNextClick(msg, fontColor:="Red", fontSize:=20, offsetX:=100, offsetY:=100, guiWidth:=500) {
    ; 1) create GUI
    Gui, InfoGui:New, , InfoGui
    Gui, InfoGui:Color, AAAAAA
    Gui, InfoGui:Font, s%fontSize% bold  c%fontColor%, Verdana
    Gui, InfoGui:Add, Text, x0 y0 w%guiWidth%, %msg%
    Gui, InfoGui:Margin, 0,0
    Gui, InfoGui:+LastFound +AlwaysOnTop -DPIScale -Caption +ToolWindow
    Gui, InfoGui:Show, NoActivate AutoSize, InfoGui                                                        ; show gui where the mouse is or stationary
    Winset, TransColor, AAAAAA 150, InfoGui

    ; 2) wait for next click or esc to delete gui again
    LButtonPressed := false
    EscPressed := false
    CoordMode, Mouse, Screen
    while !(LButtonPressed or EscPressed) {
        MouseGetPos, mx, my
        wx := mx + offsetX
        wy := my + offsetY
        WinMove, InfoGui, , %wx%, %wy%
        LButtonPressed := GetKeyState("LButton", "P")
        EscPressed := GetKeyState("Escape", "P")
        Sleep, 20
    }
    Gui, InfoGui:Destroy
}
It works, but not always. Sometimes I have to click two or three times, before the gui is destroyed. Any hints on how to correct this? Greets

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

Re: Gui follows mouse and shows information until the user clicks - click not always working

Post by mikeyww » 26 Mar 2023, 08:46

Hi,

Instead of checking the key state, you can just use a hotkey. That will be more reliable.

Code: Select all

#Requires AutoHotkey v1.1.33
Global offX, offY, winTitle := "InfoGui ahk_class AutoHotkeyGUI ahk_exe AutoHotkey.exe"

show("This is a test",,, offX := 100, offY := 100)

#If WinExist(winTitle)
~LButton::
~Esc::
SetTimer move, Off
Gui InfoGui:Destroy
Return
#If

show(msg, fontColor := "Red", fontSize := 20, offsetX := 100, offsetY := 100, guiWidth := 500) {
 Static txcolor := "AAAAAA"
 Gui InfoGui:New, +LastFound +AlwaysOnTop -Caption +ToolWindow
 Gui Margin, 0,0
 Gui Color, % txcolor
 Gui Font, s%fontSize% Bold c%fontColor%, Verdana
 Gui Add, Text, w%guiWidth%, %msg%
 Gui Show, NoActivate, InfoGui
 WinSet TransColor, % txcolor " " 150
 SetTimer move, 100
}

move() {
 MouseGetPos x, y
 WinMove, % winTitle,, x + offX, y + offY
}

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

Re: Gui follows mouse and shows information until the user clicks - click not always working

Post by Spitzi » 27 Mar 2023, 06:45

Hi @mikeyww . thanks for your answer.

Is there another way without using timers and hotkeys? I was thinking of a solution like that, but I would like to keep it as simple as possible. Just one function would be nice, no supporting code around it

Greets Simon

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

Re: Gui follows mouse and shows information until the user clicks - click not always working

Post by mikeyww » 27 Mar 2023, 08:31

You can move the second function into the first function as a labeled subroutine. You could use InputHook inside your function to capture key presses and do something with them.

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

Re: Gui follows mouse and shows information until the user clicks - click not always working

Post by Spitzi » 28 Mar 2023, 01:30

InputHook would solve it, but it only captures keystrokes and not mouseclicks

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

Re: Gui follows mouse and shows information until the user clicks - click not always working

Post by Spitzi » 28 Mar 2023, 03:26

Hi, @mikeyww. I ended up using your approach with hotkeys, instead of InputHook or other stuff. Works very reliably.

For anyone wanting something similar: here's the code that I currently use

Code: Select all

; shows a transparent info gui that follows the mouse, to tell the user what to do for next mouse click
; - msg: the message to show
; - fontColor: the color of the text
; - fontSize: the size of the text
; returns true if the user clicked, false if escape was pressed
ShowInfoGuiUntilNextClick(msg, fontColor:="Red", fontSize:=20, offsetX:=100, offsetY:=100, guiWidth:=500) {
	global showInfoGui := true
	global infoGuiQuitKey

	; 1) create GUI
	Gui, InfoGui:New, , InfoGuiWinTitle
	Gui, InfoGui:Color, AAAAAA
	Gui, InfoGui:Font, s%fontSize% bold  c%fontColor%, Verdana
	Gui, InfoGui:Add, Text, x0 y0 w%guiWidth%, %msg%
	Gui, InfoGui:Margin, 0,0
	Gui, InfoGui:+LastFound +AlwaysOnTop -DPIScale -Caption +ToolWindow
	Gui, InfoGui:Show, NoActivate AutoSize, InfoGuiWinTitle													; show gui where the mouse is or stationary
	Winset, TransColor, AAAAAA 150, InfoGui

	; 2) wait for next click or esc to delete gui again
	CoordMode, Mouse, Screen
	while showInfoGui {
		MouseGetPos, mx, my
		wx := mx + offsetX
		wy := my + offsetY
		WinMove, InfoGuiWinTitle, , %wx%, %wy%
		Sleep, 30
	}
	Gui, InfoGui:Destroy
	return (infoGuiQuitKey == "LButton")
}

#if WinExist("InfoGuiWinTitle")
	~LButton:: QuitInfoGui("LButton")
	Esc:: QuitInfoGui("Esc")
#if ;WinExist("InfoGUI")

QuitInfoGui(quitKey) {
	global showInfoGui
	global infoGuiQuitKey

	showInfoGui := false
	infoGuiQuitKey := quitKey
}
Greets

Post Reply

Return to “Ask for Help (v1)”