バインド

バインドは、キーボードやマウス等のイベントに対したアクションを記述する機能です。
以下の例は、ボタンにカーソルを持って行くとPushの文字が赤色に変化し、 ボタンからカーソルを外すとPushの文字が黒色に戻ります。

pack [button .btn -text Push -command exit]

bind .btn <Enter> {
    %W configure -fg red
}

bind .btn <Leave> {
    %W configure -fg black
}

bindの第1引数は、Widgetのパス名かWindow名またはallを指定します。
bindスクリプト内の%Wは、ここでは、.btnと同じ意味になります。

例えば、以下のようにWindow名(Button)を指定すると全てのボタンに対して のバインドとなります。
allを指定するとすべてのWindowが対象になります。

bind Button <Enter> {
    %W configure -fg red
}

バインドで扱えるイベントの種類です。
Activate	Enter		Map
ButtonPress, Button		Expose		Motion
ButtonRelease	FocusIn		MouseWheel	
Circulate	FocusOut	Property
Colormap	Gravity		Reparent
Configure	KeyPress, 	Key		Unmap
Deactivate	KeyRelease	Visibility
Destroy		Leave

使える修飾子です。
Control	Mod2, M2
Shift	Mod3, M3	
Lock	Mod4, M4
Button1, B1	Mod5, M5	
Button2, B2	Meta, M
Button3, B3	Alt
Button4, B4	Double
Button5, B5	Triple
Mod1, M1

イベントと修飾子を組み合わせて使います。
# 左ボタンをシングルクリックしたら
bind tag <Button-1> {}

# 左ボタンをダブルクリックしたら
bind tag <Double-Button-11> {}

# 左ボタンをトリプルクリックしたら
bind tag <Triple-Button-11> {}

# Enterキーを押したら
bind tag <Key-Return> {}

仮想イベント

プラットフォーム依存を吸収するために仮想イベントという機能があります。
たとえば、クリップボードへのコピーを行うのに、UNIXはマウスの左ボタン、WindowsはCtrl+Cを使います。プログラムでコピーをするためのバインドを行うときに、コピーという仮想イベントがあると プログラムが汎用的に書けます。

以下は予約されている仮想イベントです。
<<PasteSelection>> <<Copy>> <<Cut>> <<Paste>>

仮想イベントのコピーを使うとバインドの記述がすっきりします。
bind . <<Copy>> {}

仮想イベントは追加することができます。
処理の中止をする仮想イベントを追加してみましょう。
if {$tcl_platform(platform) == "unix"} {
    event add <<Cancel>> <Control-c>
} elseif {$tcl_platform(platform) == "windows"} {
    event add <<Cancel>> <Escape>
} elseif {$tcl_platform(platform) == "macintosh"} {
    event add <<Cancel>> <Command-.>
}