3 Replies Latest reply on Jul 23, 2011 4:46 AM by Ned Murphy

    Help detecting a simultaneous mouse click and keyboard imput.

    Celebrinborn

      I am making the board game Shogi in actionscript 2 and I have a question on how to detect a particular imput.

      I have a movie clip on the stage, I want it so that if I hold down the keyboard button "p" and then click on the movie clip, it will go to the movie clip's frame 2 (not the entire flash project's frame 2, just the particular icon's frame 2. I need this because I am using movie clips as game pieces and in Shogi, certain game pieces can be promoted. I also was wondering how to detect what frame it is on. Thanks everyone.

       

      PS: I have very little experience with either flash or actionscript however I use c++ a lot and use MS Excel's formulas to make simple programs.

        • 1. Re: Help detecting a simultaneous mouse click and keyboard imput.
          Ned Murphy Adobe Community Professional & MVP

          Here's code that does what you described...

           

          var pDown = false;

           

          var keyListener:Object = new Object();
          Key.addListener(keyListener);

           

          keyListener.onKeyDown = function(){
             if(Key.getCode() == 80){  // key p
               pDown = true;
             }
          }

           

          keyListener.onKeyUp = function(){
             if(Key.getCode() == 80){  // key p
               pDown = false;
             }
          }

           

          mcName.onRelease = function(){  // the mc click code
             if(pDown){
               mcName.gotoAndStop(2);
             }
             trace(mcName._currentframe); // to tell which frame it is on
          }

          • 2. Re: Help detecting a simultaneous mouse click and keyboard imput.
            Celebrinborn Level 1

            I think I understand most of this, however I am having trouble understanding this part:

            keyListener.onKeyDown = function(){

               if(Key.getCode() == 80){  // key p

                 pDown = true;

               }

            }

            could you plaese explain this to me? Most importantly, //key p is a comment so it is ignored, how does the computer know to listen for the p key and not another key? Also, I am assuming that I have to change the mcName parts to pawn_1 because the name of the instance that I am writing code for is called pawn_1, are there any other parts of the code I have to edit? I'm sorry for having to ask such noobish questions but I just started using flash.

            • 3. Re: Help detecting a simultaneous mouse click and keyboard imput.
              Ned Murphy Adobe Community Professional & MVP

              "Key.getCode() == 80"  -  80 is the key code for the "p" key on the keyboard.  The comment is simply to indicate which key the line is testing for

               

              If you end up needing to detect other keys, you can add the following to the keyDown function to detect which codes represent which keys by pressing the key while testing...  trace(Key.getCode());

               

              While testing, in the player you will need to disable the keyboard shortcuts (the keybord shortcuts that Flash has set up for the editor).  So when you initiate a test, in the player choose Control -> Disable Keyboard Shortcuts.

               

              Whatever you named the instance of your movieclip is represented by mcName, so change it as needed.