1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Autohotkey で SetTimer に引数付きの関数を実行させる

Posted at

例えば AutoHotkey Wiki の tooltip の記事

example_1
ToolTip, Multiline`nTooltip, 100, 150
; To have a ToolTip disappear after a certain amount of time
; without having to use Sleep (which stops the current thread):
# Persistent
ToolTip, Timed ToolTip`nThis will be displayed for 5 seconds.
SetTimer, RemoveToolTip, 5000
Return
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
Return

とあります。Sleep 等のコマンドで待機するとその間他のスレッドの動作に影響が出る / 影響されるため、SetTimer で指定時間後に ToolTip を非表示にするラベルを実行する方が良いと判断してのことだと思います。

上記を簡略化したものが以下になります。

example_2
# Persistent
ToolTip, text, 100, 150
SetTimer, RemoveToolTip, -5000
Return

RemoveToolTip:
ToolTip
Return

ToolTip が一つの場合はこれで良いのですが、複数個の ToolTip をそれぞれ設定した時間だけ表示させたい場合には工夫が必要になります。
ところで、SetTimer はラベルのほかに関数を実行することもできます。

example_3
# Persistent
ToolTip, text, 100, 150
SetTimer, RemoveToolTip, -5000
Return

RemoveToolTip(){
    ToolTip
    Return
}

SetTimer の第一引数には任意の function object を指定することができるため、次のように書くことができます。

example_4
# Persistent
ToolTip, text, 100, 150
RemoveToolTip_func := Func("RemoveToolTip")
SetTimer, %RemoveToolTip_func%, -5000
Return

RemoveToolTip(){
    ToolTip
    Return
}

AutoHotkey v1.1.20 から function object の派生として boundfunc object というものが存在します。これは引数が定められた function object として扱うことができます。これと SetTimer を組み合わせることで、引数を指定した関数を任意時間経過後に実行することができます。

example_5
# Persistent
ToolTip, text_1, 100, 150, 1
ToolTip, text_2, 300, 150, 2
RemoveToolTip_func_1 := Func("RemoveToolTip").bind(1)
RemoveToolTip_func_2 := Func("RemoveToolTip").bind(2)
SetTimer, %RemoveToolTip_func_1%, -8000
SetTimer, %RemoveToolTip_func_2%, -5000
Return

RemoveToolTip(index){
    ToolTip, , , , %index%
    Return
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?