2 Replies Latest reply on Dec 9, 2015 10:34 AM by nezarov

    How Do You Get 2 Key press At The Same Time?

    ra_linder@hotmail.com

      How Do You get 2 keys pressed at the same time?

       

      this works for 1 Key!

       

      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

        function keyPressed(evt: KeyboardEvent): void

             {

                  trace(evt);

                  switch (evt.keyCode)

             {

                       case 38:

                            trace("UP");

                            break;

                       case 39:

                            trace("Right");

                            break;

                       case 37:

                            trace("Left");

                            break;

                       case 40:

                            trace("Down");

                            break;

             }

        }

       

      but if i add for example

                     case 38 & 39:

                            trace("UP Right");

                            break;

                      case 38 & 37:

                            trace("UP Left");

                            break;

       

       

      to each they do not work How Can this be done?

        • 1. Re: How Do You Get 2 Key press At The Same Time?
          ClayUUID Adobe Community Professional

          First, Googling for "Flash multiple key press" yields many, many code samples that show how to do exactly what you want. Here's one of them: Flash Multiple Key Press Detection.

           

          Second, the switch statement (and variable comparison in general) does not work that way. Every case in a switch only checks for a single value. "38 & 39" just performs a bitwise AND of those two numbers, then compares the result to the key code (which can only be a single value at a time, not two values at once).

          1 person found this helpful
          • 2. Re: How Do You Get 2 Key press At The Same Time?
            nezarov Level 4

            var _keys:Object = {};

             

            stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);

            stage.addEventListener(KeyboardEvent.KEY_UP, onUp);

             

            function onDown(e:KeyboardEvent): void {

                _keys[e.keyCode] = true;

                if (_keys[Keyboard.UP] && _keys[Keyboard.RIGHT]) {

                    trace("UP Right");

                }

               if (_keys[Keyboard.UP] && _keys[Keyboard.LEFT]) {

                    trace("UP Left");

                }

            }

             

            function onUp(e:KeyboardEvent): void {

                _keys[e.keyCode] = false;

            }