Is there an easy way to add a delay to an event? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Is there an easy way to add a delay to an event?

21 Oct 2021, 00:06

Let's say the following, Ctrl_Change only continues after 1 second with no change
Gui_Size continues only after releasing the mouse

Code: Select all

mygui := gui("Resize")
mygui.OnEvent("size", Gui_Size)
test := mygui.add("edit","w400 h30")
test.OnEvent("change",Ctrl_Change)
mygui.show

Ctrl_Change(GuiCtrlObj, *)
{
    MsgBox(GuiCtrlObj.Value)
}

Gui_Size(GuiObj, MinMax, Width, Height)
{
    mygui.GetPos(, ,&w,&h)
    test.Move(, ,w-40,h-50)
}
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 01:59

I'm afraid I'm not entirely sure of what you are trying to do.

Is Sleep not what you are looking for?

What is your use case for this delay?
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 02:10

@TheArkive

Ctrl_Change needs to search and then display the data
The process of searching and displaying takes some time
Because each character entered will trigger an event
So I need to add a delay to make sure that the input is complete (no re-entry within a second )


The Gui_Size event is similar, as it requires redrawing the interface, the interface will lag and flicker slightly.
I want to redraw the interface after the mouse is released
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 02:19

In the case of Ctrl_Change, as you know, it fires with every keystroke, but you are looking for some other kind of event. The script will have no concept of what "complete input" looks like unless you code in those parameters. One (old) way of determining "complete input" is to wait for the user to press {ENTER}. You can scan the edit text on every keystroke. When `n is detected, then you can execute whatever your code is (unless your incomplete input is intended to contain `n).

I'm not sure about Gui_Size at the moment. You may be able to set some window styles to achieve your desired resize effect.
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 02:43

@TheArkive
Ctrl_Change
I am currently removing the change event from edit
and then added a hidden default button and bind it to this
which is the response to Enter


It seems I'm overthinking it, so maybe

Code: Select all

Ctrl_Change(GuiCtrlObj, *)
{
	static TimeSave := ""
	if TimeSave
	ToolTip A_TickCount-TimeSave
	if TimeSave && A_TickCount-TimeSave > 1000
		MsgBox(GuiCtrlObj.Value), TimeSave := ""
	Else 
		TimeSave := A_TickCount
}

Still not quite right, the last input will not trigger if it is within one second

I still have to find a way to determine if it's the last input ....
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Is there an easy way to add a delay to an event?  Topic is solved

21 Oct 2021, 04:57

Code: Select all

#Requires AutoHotkey v2.0-beta.2

G := Gui()

Edit1 := G.Add('Edit')
Edit1.OnEvent('Change', (*) => SetTimer(showWhatYouTypedAfter1s, -1000))
showWhatYouTypedAfter1s() => ToolTip(Edit1.Value)

G.Show()
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 05:15

@swagfag

Thank you very much
I have tried this method before and it seems that the SetTimer function is repeated,
However, the documented SetTimer function does not run repeatedly
I think I wrote it wrong
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 07:13

Wouldn’t you want to track the timer and destroy it if another change event occurs within that one second?
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 10:39

@kczx3

Yes, I used to write the same thing probably?
But the function called is repeated every time the event is triggered
And the answer way I tested other things like the msgbox fileapped would be destroyed before execution
So the last SetTimer within one second is destroyed by the current SetTimer, until the last one is triggered after exactly -1000ms.
I think I should have written it wrong before
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 11:11

I can’t quite follow your English, apologies. I think what you’re saying is that only one timer can be active per function object. Is that right?
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 11:19

until the last one is triggered after exactly -1000ms
possibly has to do with this
If Period is 0, the timer is marked for deletion. 👉 If a thread started by this timer is still running 👈, the timer is deleted after the thread finishes (unless it has been reenabled); otherwise, it is deleted immediately. In any case, the timer's previous Period and Priority are not retained.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 11:28

Nice, that's handy and good to know!
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Is there an easy way to add a delay to an event?

21 Oct 2021, 11:32

@kczx3
sorry for my online translation in English..
One timer, multiple runs
The answer is correct, I have no problem now, I wrote it wrong before

Code: Select all

SetTimer test,-1000 ;1
SetTimer test,-1000 ;2 destroy 1
SetTimer test,-1000 ;3 destroy 2
SetTimer test,-1000 ;4 destroy 3
SetTimer test,-1000 ;5 destroy 4,runthis


test()
{
	MsgBox 1
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: a_bolog, Descolada, IfThenElse, WarlordAkamu67 and 32 guests