事件处理器 http://reference.wolfram.com/mathematica/ref/EventHandler.html可用于捕获各种鼠标事件(鼠标向上、鼠标向下、鼠标单击、鼠标拖动)。使用 MousePosition 来添加一些智能。
Example:
DynamicModule[{col1 = Green, col2 = Blue}, Graphics[
{
EventHandler[
Dynamic[{col1, Disk[]},
ImageSize ->
Tiny], {"MouseClicked" :> (col1 =
col1 /. {Red -> Green, Green -> Red})}],
EventHandler[
Dynamic[{col2, Disk[{1, 1}]},
ImageSize ->
Tiny], {"MouseClicked" :> (col2 =
col2 /. {Blue -> Yellow, Yellow -> Blue})}]
}
]
]
可以单独单击圆圈。为每个对象单独定义一个操作。
令人惊讶的是,这甚至适用于 3D 图形:
DynamicModule[{col1 = Green, col2 = Blue},
Graphics3D[
{
EventHandler[
Dynamic[{col1, Sphere[]},
ImageSize ->
Tiny], {"MouseClicked" :> (col1 =
col1 /. {Red -> Green, Green -> Red})}],
EventHandler[
Dynamic[{col2, Sphere[{1, 1, 1}]},
ImageSize ->
Tiny], {"MouseClicked" :> (col2 =
col2 /. {Blue -> Yellow, Yellow -> Blue})}]
}
]
]