7 Replies Latest reply on May 4, 2009 8:24 PM by Desprez10

    Key.isDown / getAscii() problems


      I'm having some trouble detecting keypresses.

      Woking with the arrow keys works fine, but using getAscii() is having problems.

      I tried using:

      if (getAscii(100)) {//do stuff;};

      but the issue is that once you press a key, getAscii() keeps returning the value of the last key pessed even after you stop pessing it.


      I tied using a keyDown listener, instead of onEnteFame and couldn't get it to work at all.

      I tried searching these foums, but some of the solutions had all the code deleted... odd.


      How do I add the ability to add key detection for the other keyboard keys to this code?


      xspeed = 0;
      yspeed = 0;

      this.onEnterFrame = function() {
           if (Key.isDown(Key.LEFT)) {
               xspeed += -3;

           if (Key.isDown(Key.RIGHT)) {
               xspeed += 3;

           if (Key.isDown(Key.UP)) {
               yspeed += -3;

           if (Key.isDown(Key.DOWN)) {
               yspeed += 3;

           xspeed *= .8;
           yspeed *= .8;
           if (Math.abs(xspeed) < 1) {xspeed = 0;};
           if (Math.abs(yspeed) < 1) {yspeed = 0;};
           if (xspeed <> 0 or yspeed <> 0) {
               _root.Map._x += xspeed;
               _root.Map._y += yspeed;



      (The code has a few exta lines to make the accelleration nice and smooth.)

        • 1. Re: Key.isDown / getAscii() problems

          try this:


          //////////********KEY LISTENER*************************
          delete keyListener;

          keyListener = new Object();
          keyListener.onKeyDown = function() {



          if (Key.getAscii() == 97) {
            trace("A hit");
          ///*************spacebar *********************
          if (Key.isDown(32)) {
            trace("space bar pressed");
          ////*********CONTROL J***********************         
          if (Key.isDown(Key.CONTROL) && Key.getCode() == 74) {

            trace("CONTROL J");
          ////********* p ***********************         
          if (Key.isDown(112)) {

          ////********* q ***********************         
          if (Key.isDown(113)) {

          if (Key.getCode() == 112) {








          Always remember to remove your listener before calling on it again.Most of the time I place it on a frame the user can NOT return to, because it will keep adding listeners and you can easly build up hundreds of them. So remeber to remove it before calling it again...just like the above code does already.

          • 2. Re: Key.isDown / getAscii() problems
            Desprez10 Level 1

            Well, I got that solution to work, but it destroyed my frame rate - to the point where I don't know if I can use it.

            Presumably because of the constant adding and deleting of the listener - it's the only real significant difference - unless getAscii() is relatively cpu intensive.


            Is there a better way to do this?


            EDIT: Hmmm. It's also bonkered the smooth accelleration. Now, when I stop pressing a movement key, it no longer smoothly accelerates to 0, but now simply just halts, untill I press another key, and then it finishes it's stop, while applying the new direction.

            • 3. Re: Key.isDown / getAscii() problems
              Zealouse Level 1

              Maybe you misunderstood me... the above code will not slow down any frame rate in the flash player and will run pretty lean.

              It only creates 1 object...the same as creating 1 button basicaly....so you must have an issue somewhere else.

              • 4. Re: Key.isDown / getAscii() problems
                Zealouse Level 1

                Use the "OnKeyUp" for smooth accelleration.


                You can still use your OnEnterframe and grab the getaskii code above if that makes things easer.

                • 5. Re: Key.isDown / getAscii() problems
                  Desprez10 Level 1

                  Well, I tried it while keeping the onEnterFrame bit, but somehow the keyDown detectionis running too many times.

                  A single key press causes it to trigger many many times, and I'm hitting and releasing the key as fast is humanly possible.

                  This is in direct contrast to my original code.


                  After further testing, the longer it remains running, the more instances of a single keypress are detected.

                  I used a variable that increments every time a particular getAscii() if condition is satisfied, and it seems to roughly correspond to the (framerate * seconds the program is running). Then, once the key is pressed it will perform the movement however many hundredes of frames have passed times.


                  This suggests to me that multiple copies of the listener are being created, yet I have included all the deletion aspects you listed.


                  Any ideas?


                  As a seperate question, were you suggesting that onKeyUp could be used instead of onEnterFrame?

                  • 6. Re: Key.isDown / getAscii() problems
                    Zealouse Level 1

                    Yeah when you press a keyy it will keep firing the onpress function, and no it is not recreating the listener...so you need to make an interlock...





                    if(key_Is_Press != true){


                    key_Is_Press = true


                    //fire off your function once




                    >>>onKeyUp or at the end of what you are doing


                    key_Is_Press = false

                    • 7. Re: Key.isDown / getAscii() problems
                      Desprez10 Level 1

                      Maybe I didn't explain it clear.

                      It acts as if it is firing the onPress BEFORE I press the key. Then, once I press the key, the trace shows me how many times the if conditional was entered, and this number seems to correspond to the number of frames that I waited before I pressed a key.