6 Replies Latest reply on Mar 14, 2007 5:25 PM by kglad

    My listeners arnt listening

    numerical07
      I tried this script


      myListener = new Object();
      myListener.onKeyDown = function () {
      trace ("You pressed a key.");
      }
      myListener.onKeyUp = function () {
      trace ("You released a key.");
      }
      Key.addListener(myListener);

      but my listners arnt responding. I tried a couple others and it wont work either. I got this script straight out of the actionscript dictionary and it still wont work. Is it because listeners are new and my flash compiler doesnt understand it. Im confused. Im using flash professional 8
        • 1. Re: My listeners arnt listening
          kglad Adobe Community Professional & MVP
          what makes you think your code isn't working? do you have your output panel's filter set at verbose?
          • 2. Re: My listeners arnt listening
            numerical07 Level 1
            I know its not working because when I compile the program. I press enter or any key at that and I get no outcome. AND Im not sure, where is that
            • 3. Re: My listeners arnt listening
              numerical07 Level 1
              ok this is funny. It does respond to any key except for my enter key. Anyone have any suggestions?
              • 4. Re: My listeners arnt listening
                kglad Adobe Community Professional & MVP
                you can't see the results of a trace() function outside the flash test environment. use something else to confirm your code is working.
                • 5. Re: My listeners arnt listening
                  numerical07 Level 1
                  nevermind I figured it out. I guess the enter key isnt considered part of a onKeyDown function. unless the program specifically calls on it. Something like this...
                  example 1.1

                  listener = new Object();
                  listener.onKeyUp = function() {
                  trace("This was message was invoked by addListener(listener); It will react to any ASCII except for the specified ones such as ENTER, BACKSPACE, DOWN and so on");
                  }
                  addListener(listener);

                  example 1.2

                  enterlistener = new Object();
                  enterlistener. onKeyUp = function() {
                  if (get.KeyCode() == key.ENTER){
                  trace("Now since I specified it, it will react to it by doing using the if statement, it reacted to the response");
                  }
                  addListener(enterlistener);

                  BELOW WILL NOT WORK
                  example1.3

                  enterlistener = new Object();
                  enterlistener.keyUp = function() {
                  if (get.KeyCode() == key.ENTER){
                  trace("Now since I specified it, it will react to it by doing using the if statement, it reacted to the response");
                  }
                  addListener(enterlistener);


                  Notice in example 1.2 the bold letters "On" its there in 1.2 but gone in 1.3. This means that if you dont have onKeyUp when you are trying to use the ENTER key or what ever, it will not work, thats what makes the difference between the right script and the wrong script
                  • 6. Re: My listeners arnt listening
                    kglad Adobe Community Professional & MVP
                    if you're trying the enter key in the test environment you need to disable keyboard shortcuts (control/disable keyboard shortcuts).