5 Replies Latest reply on Jan 10, 2007 6:39 AM by Newsgroup_User

    Are Key Listener combinations possible?

    Angyl Level 1
      How do I get a key listener to work with a combination of keys (like Shift+M) rather than a single key? I'm using the keyCode method currently but with the project I'm working on that forces me to map out a lot of the keyboard and gets kind of confusing.

      A snippet of my code is this:

      keyListener1.onKeyDown = function() {
      if (Key.isDown(77)) {
      carrier1.carrierJet.gotoAndPlay(2);
      delete this.onKeyDown
      }

      That works if I press M. How would I code it to work only if I press SHIFT+M?
        • 1. Re: Are Key Listener combinations possible?
          Level 7
          Angyl,

          > How do I get a key listener to work with a combination of
          > keys (like Shift+M) rather than a single key?

          Aha. Well, the answer may come as a surprise, but only because it's so
          obvious, once you see it. :)

          > A snippet of my code is this:
          >
          > keyListener1.onKeyDown = function() {
          > if (Key.isDown(77)) {

          Here, you're listening for the M key, represented internally by the
          number 77. You want to listen for the Shift key *in addition* to the M key,
          so you'll need to use the logical AND operator in your if() statement.
          Shift is key code 16 -- you can find them all under "Keyboard Keys and Key
          Code Values" in the ActionScript 2.0 Language Reference" -- so this would be
          your if() statement ...

          if (Key.isDown(77) && Key.isDown(16)) { ... }

          Make sense?


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Are Key Listener combinations possible?
            ggshow Level 2
            David Stiller >
            this code
            if (Key.isDown(77) && Key.isDown(16)) { ... }
            works for Shift+M, and works for M+Shift as well

            how about this one:

            var myListener:Object = new Object();
            myListener.onKeyDown = function() {
            if (Key.isDown(77)) {
            if (_root.key == "Shift") {
            carrier1.carrierJet.gotoAndPlay(2);
            delete this.onKeyDown;
            }
            } else {
            if (Key.isDown(16)) {
            _root.key = "Shift";
            }
            }
            };
            myListener.onKeyUp = function() {
            _root.key = "";
            };
            Key.addListener(myListener);
            • 3. Re: Are Key Listener combinations possible?
              Level 7
              ggshow,

              > if (Key.isDown(77) && Key.isDown(16)) { ... }
              > works for Shift+M, and works for M+Shift as well

              That's right.

              > how about this one:
              >
              > var myListener:Object = new Object();
              > myListener.onKeyDown = function() {
              > if (Key.isDown(77)) {

              Checking for M, so far.

              > if (_root.key == "Shift") {

              Now checking the value of a variable in the main timeline. If the value
              of this variable is the string "Shift", then ...

              > carrier1.carrierJet.gotoAndPlay(2);
              > delete this.onKeyDown;
              > }

              ... a movie clip by the instance name carrierJet is told to go to frame 2
              and play. This carrierJet is nested inside another movie clip with the
              instance name carrier1 (or carrier1 is some other object with a property
              that points to carrierJet).

              If if above is not true ...

              > } else {
              > if (Key.isDown(16)) {
              > _root.key = "Shift";

              ... then *if* one of the shift keys is pressed, set that root variable to
              the string "Shift".

              Well ... to be honest, I'm not sure what that key variable is for. If I
              understood right, Angyl wanted to test for the condition of Shift and the
              key M at the same time. If so, the simplest route is to simply check for
              both those keys. I'm not sure why a variable help matters, but maybe you
              could explain?


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: Are Key Listener combinations possible?
                ggshow Level 2
                just trying to test either user press the Shift key 1st...

                should have some other better ways to do that..

                or... if in the application, "which key pressed 1st" is not important, then your code is just perfect...
                • 5. Re: Are Key Listener combinations possible?
                  Level 7
                  ggshow,

                  > just trying to test either user press the Shift key 1st...

                  Aha. Okay, that makes sense.

                  > should have some other better ways to do that..

                  Ehh, you work with what you have, right? That's one of the fun
                  challenges of programming: if the API doesn't offer it, roll your own. :)


                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."