4 Replies Latest reply on Nov 19, 2007 2:45 AM by amaIain

    Attaching Scripts to Buttons

    amaIain
      I have a button displayed as Zoom In in the Basket panel:

      HERE

      When I attach this script to the button:

      on (release, keyPress "<Space>") {

      gotoAndPlay(2);
      }

      The mouse release works fine but when I try the keyboard for keyPress, nothing happens. Anyone got any ideas as to what I am doing wrong?
        • 1. Re: Attaching Scripts to Buttons
          kaeptn-q
          hi,

          i think handling keyboard-input works different ->

          1. there is (as far as i know) no Event "keyPress" (they are "onKeyDown" and "onKeyUp")

          2.You´ll first have to create a Listener-Object which has functions "onKeyDown" or "onKeyUp" or both:
          var objListener = new Object();
          objListener.onKeyDown = function(){
          if(Key.isDown(Key.SPACE){
          // do whatever
          }
          }

          3. You have to register that Obj as Listener to the Keyboard:
          Key.addListener(objListener);

          that should do.

          cheers,
          tom

          • 2. Re: Attaching Scripts to Buttons
            amaIain Level 1
            2.You´ll first have to create a Listener-Object which has functions "onKeyDown" or "onKeyUp" or both:
            var objListener = new Object();
            objListener.onKeyDown = function(){
            if(Key.isDown(Key.SPACE){
            // do whatever
            }
            }

            Do I create this in the first frame?

            3. You have to register that Obj as Listener to the Keyboard:
            Key.addListener(objListener);

            Where do I add this?

            Forgive me, I'm a total novice at Flash. It was the script assist function that gives me the keyPress event.

            thanks
            • 3. Re: Attaching Scripts to Buttons
              kaeptn-q Level 1
              i tried - and you are right.

              the global "on()"-procedure seems to know a "keyPress" event.

              i created a button and gave it this script:
              on(keyPress "<Space>", rollOver, release){
              trace("lulul");
              }
              an it works in all 3 cases.

              important: the script has to be attached to the button itself, NOT the timeline.

              may that´s the trick....

              cheers,
              t
              • 4. Re: Attaching Scripts to Buttons
                amaIain Level 1
                yep, it was attached to the button. I'm puzzled because google searches tell me the code is fine. The release event works fine but the keyPress just dosent want to know. I've even complied the movie for Flash Player 9.

                I tried the script you gave me, i get some strange results with it.