Listing 5

DEF SCR Script {
      eventIn SFFloat set_fraction
      field SFInt32 curchoice 0
      eventOut SFInt32 choice
      url "javascript:
         function set_fraction(val,time) {
            if(val < 0.6 && curchoice == 0) {
               choice = 1;
               curchoice = 1;
         } else if(val >= 0.6 && curchoice == 1) {
               choice = 0;
               curchoice = 0;
            }
         }
      "
   }
 g DEF BUTTON TouchSensor { }
   DEF SW Switch {
    gchoice [
      Shape {
       geometry Box { }
       appearance Appearance { material DEF MAT
          Material { } }
      }
      Shape {
       geometry Sphere { radius 0.5 }
       appearance Appearance { material Material
          { diffuseColor 0 0 0.8 } }
      }
    g]
   }
   ROUTE TS.fraction_changed TO SCR.set_fraction
   ROUTE SCR.choice TO SW.whichChoice