Skip to main content

dynamic - How to change the color of a button while it is pressed



I want to change the color of a button while it is pressed, pause for a short time and then change the color back to the original color.


I can't seem to figure out how to do this (tried various forms of DynamicModule and Dynamic, but no luck). This is what I tried so far:


Manipulate[
DynamicModule[{simulationFlag, bgnd = Automatic},
simulationFlag = newSimulation;
Pause[0.5];
Dynamic[bgnd = Automatic; {simulationFlag}]],
{{newSimulation, 0.02}, ControlType -> None},
Row[{
Button["New Simulation", Dynamic[newSimulation = newSimulation+1; {bgnd=Yellow}],

Background -> Dynamic[bgnd]]}]
]

Answer



For some unknown reason, @István's answer doesn't work here, so I rolled my own:


DynamicModule[{c = Yellow}, 
Dynamic@Button["Press", Print["Pressed"];
c = Red;
RunScheduledTask[c = Yellow;
RemoveScheduledTask[$ScheduledTask], {0}, AbsoluteTime[] + 3],
Background -> c]]

Comments