7 Replies Latest reply on May 11, 2009 6:07 PM by Ned Murphy

    key listener ASWD?

    shintashi Level 1

      I'm trying to learn how to do alphabet keyboard keylisteners, instead of arrowkeys, spacebar, and so on. I'm actually going to need to do the whole alphabet, and I'm guessing there's a way for the computer to "get" the right key codes for letters, and then input that code so it responds like any other

       

      if (Key.isDown(Key.MyKey)){ program}.

       

      Is there an example somewhere of a handful of alphabet keys or ASWD for actionscript? I've got this

       

      keyListener = new Object();
      keyListener.onKeyDown=function(){
         if(Key.isDown(Key.ENTER)){
            go_mc.onRelease();
         }
      }
      Key.addListener(keyListener);

       

      but I'm looking to know how to use letters, like a,b,c, etc. I found this map

      Asciilettercode
      49149
      50250
      51351
      52452
      53553
      54654
      55755
      56856
      57957
      48048
      45-189
      61=187
      92\220
      113q81
      119w87
      101e69
      114r82
      116t84
      121y89
      117u85
      105i73
      111o79
      112p80
      91[219
      93]221
      97a65
      115s83
      100d68
      102f70
      103g71
      104h72
      106j74
      107k75
      108l76
      59;186
      39'222
      122z90
      120x88
      99c67
      118v86
      98b66
      110n78
      109m77
      44,188
      46.190
      47/191

       

      but I don't know where to type the numbers, which of the two numbers to use, or if the string (listed for ENTER) has to be changed in some way.

        • 1. Re: key listener ASWD?
          kglad Adobe Community Professional & MVP

          the key class has both getAscii() and getCode() methods.

          • 2. Re: key listener ASWD?
            shintashi Level 1

            I've tried this four different ways

            #1

            onEnterFrame = function(){
            keyListener = new Object();
            keyListener.onKeyDown=function(){
               if (Key.getCode() == 97){
                  box_mc._rotation += 25;
               }
            }
            Key.addListener(keyListener);
            }

             

            #2

            onEnterFrame = function(){
            keyListener = new Object();
            keyListener.onKeyDown=function(){
                if (Key.getCode() == 65){
                   box_mc._rotation += 25;
                }
            }
            Key.addListener(keyListener);
            }

             

            and the previous two without onenterFrame. Pressing "a" does nothing.

            • 3. Re: key listener ASWD?
              Ned Murphy Adobe Community Professional & MVP

              I don't think the onEnterFrame code is giving you anything but an overload of listeners and objects.  When you are testing the file in Flash, you need to disable the keyboard shortcuts in the player (Control -> Disable Keyboard Shortcuts) in order to have all of the keys working for you.  Without that, only the general function keys (ENTER, LEFT, RIGHT, etc) function.

              • 4. Re: key listener ASWD?
                shintashi Level 1

                Domo aregato! I never knew about disabling the keyboard thing. And managed to fix the way the onEnter is used.

                 

                 

                 

                keyListener = new Object();
                keyListener.onKeyDown=function(){
                   if (Key.getCode() == 65){onEnterFrame = function(){
                      box_mc._rotation += 25;}
                   }
                }
                keyListener.onKeyUp =function(){
                   if (Key.getCode() == 65){onEnterFrame = function(){
                      box_mc._rotation += 0;}
                   }

                  
                }
                Key.addListener(keyListener);

                • 5. Re: key listener ASWD?
                  Ned Murphy Adobe Community Professional & MVP

                  On the keyUp event I'm assuming you want to stop the rotation.  What you should do is remove the onEnterFrame event instead of creating another...

                   

                  keyListener.onKeyUp =function(){
                     if (Key.getCode() == 65){
                          delete onEnterFrame;
                     }  
                  }

                  • 6. Re: key listener ASWD?
                    shintashi Level 1

                    I ran into another problem when using multiple keylisteners for the same object, it seems if you turn on one, that's the one that is listened to and the other becomes stuck or non functional. For example, the letters "s" and "a". Whichever you click first becomes dominant. I never have this problem with arrow keys.

                     

                    keyListener = new Object();
                    keyListener.onKeyDown=function(){
                       if (Key.getCode() == 65){onEnterFrame = function(){
                          box_mc._rotation += 25;}
                       }
                    }
                    keyListener.onKeyUp =function(){
                       if (Key.getCode() == 65){
                      delete onEnterFrame;}
                    }

                     

                      
                    keyListener.onKeyDown=function(){
                       if (Key.getCode() == 83){onEnterFrame = function(){
                          box_mc._rotation -= 25;}
                       }
                    }
                    keyListener.onKeyUp =function(){
                       if (Key.getCode() == 83){delete onEnterFrame;}
                    }
                      
                      

                     

                    Key.addListener(keyListener);

                    • 7. Re: key listener ASWD?
                      Ned Murphy Adobe Community Professional & MVP

                      Actually, the way you've done it, the one you code last that wins.  You can't assign the same event function to the same listener in two different places with different functionality and have both working.  You need one event function that processes all.

                       

                      keyListener = new Object();


                      keyListener.onKeyDown=function(){

                       

                         var keyUsed = Key.getCode();


                         if (keyUsed == 65){

                             onEnterFrame = function(){
                                box_mc._rotation += 25;

                             }
                         } else if(keyUsed == 83){

                             onEnterFrame = function(){
                                 box_mc._rotation -= 25;

                             }
                         }
                      }

                       

                      keyListener.onKeyUp =function(){
                        delete onEnterFrame;
                      }
                        
                      Key.addListener(keyListener);

                      1 person found this helpful