This is a crude example:
Code: Select all
#If Value := Test()
#If
Test()
{
return 15
}
Test_Handler(arg)
{
msgbox, % arg
}
^t::
Hotkey, If, Value := Test()
fn := Func("Test_Handler").Bind(Value)
Hotkey,F11,% fn, on UseErrorLevel
return
Code: Select all
#If Value := Test()
#If
Test()
{
return 15
}
Test_Handler(arg)
{
msgbox, % arg
}
^t::
Hotkey, If, Value := Test()
fn := Func("Test_Handler").Bind(Value)
Hotkey,F11,% fn, on UseErrorLevel
return
Code: Select all
#If % test_fn
#If
Test(Obj)
{
return Obj[1] := 15
}
Test_Handler(arg)
{
msgbox, % arg[1]
}
^t::
MyFunc() {
static Obj := []
, test_fn := Func("Test").Bind(Obj)
, fn := Func("Test_Handler").Bind(Obj)
Hotkey, If, % test_fn
Hotkey, F11, % fn, On
}
the above approach doesnt protect against this. it's basically the same as using a global variable, except it just uses objects instead. depending on circumstances and the script's contents, ahk may call into ur condition at times u wouldnt normally expect it to. so, if that happens and the hotkey procedure is still running currently, it could potentially start working with different data halfway through.doubledave22 wrote: ↑04 May 2021, 17:27can lead to problems if "Value" is changed and some Test_Handler calls are queued up. I'd like to send exact arguments to Test_Handler that are returned from the Test() function (ideally this will be an object)
What is it that you think this will do?
Yeah, my example can be changed like this:can lead to problems if "Value" is changed and some Test_Handler calls are queued up
Code: Select all
Test(Obj)
{
static val := 3
--val
if val
Obj.Push(val)
return val
}
Test_Handler(arg)
{
Critical
Sleep, 2000
ToolTip, % arg.RemoveAt(1)
}
^t::
MyFunc() {
static Obj := []
, test_fn := Func("Test").Bind(Obj)
, fn := Func("Test_Handler").Bind(Obj)
Hotkey, If, % test_fn
Hotkey, F11, % fn, On
}
Code: Select all
Test(Obj)
{
static testvar1 := 0
testvar1++
Sleep, 500
val := {1: testvar1, 2: a_thishotkey}
Obj.Push(val)
return 1
}
Test_Handler(arg)
{
Critical
output := arg.RemoveAt(1)
ToolTip, % output.1 " and " output.2
}
^t::
MyFunc() {
static Obj := {}
, test_fn := Func("Test").Bind(Obj)
, fn := Func("Test_Handler").Bind(Obj)
Hotkey, If, % test_fn
Hotkey, F11, % fn, On
Hotkey, F10, % fn, On
}
Evaluation of the #If (and it returning true) does not necessarily mean that the hotkey is being fired, and even if it is, the hotkey's subroutine might not be called; for instance, if #MaxThreadsPerHotkey prevents it.Note: Scripts should not assume that the expression is only evaluated when the key is pressed (see below).
Source: #If - Syntax & Usage | AutoHotkey
Code: Select all
#If Test()
F11::MsgBox Something went wrong.
Test() {
static count := 0
Hotkey If, Test()
fn := Func("Test_Handler").Bind({n: ++count})
Hotkey F11, % fn
return true
}
Test_Handler(context) {
MsgBox % context.n
}
Users browsing this forum: No registered users and 341 guests