1

im tryin to figure out condition AND for "shortcut" for quitting standalone app from flash. I would like to push 2 keys and combination of these two keys "C+M" should quit my app. Heres my code but its still not working. I tryed to make shure that app allow me to push multiple buttons at the same time and after that I created the function for quitting. Any answers be great.

var keyPressedC:Boolean;  
var keyPressedM:Boolean;       

addEventListener(KeyboardEvent.KEY_DOWN, check_key_down,false,0,true);
addEventListener(KeyboardEvent.KEY_UP, check_key_up,false,0,true);
addEventListener(Event.ENTER_FRAME, check_keys,false,0,true);

function check_keys(event:Event):void
{
    if(keyPressedC)
      trace("pushed C")
    if(keyPressedM)
      trace("pushed M")
}

function check_key_down(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = true;
    if(event.keyCode == 77)
        keyPressedM = true;
   }

function check_key_up(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = false;
    if(event.keyCode == 77)
        keyPressedM = false;
}
   
import flash.system.fscommand;

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterKeyHandlercm);
 function enterKeyHandlercm(event:KeyboardEvent):void 
{
 if (event.keyCode == Keyboard.C && event.keyCode == Keyboard.M)
 {
  fscommand("quit");
 }
}

Edited, still not working:

var keyPressedC:Boolean;  
var keyPressedM:Boolean;       

addEventListener(KeyboardEvent.KEY_DOWN, check_key_down,false,0,true);
addEventListener(KeyboardEvent.KEY_UP, check_key_up,false,0,true);
addEventListener(Event.ENTER_FRAME, check_keys,false,0,true);

function check_keys(event:Event):void
{
    if(keyPressedC)
      trace("pushed C")
    if(keyPressedM)
      trace("pushed M")
}

function check_key_down(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = true;
    if(event.keyCode == 77)
        keyPressedM = true;
   }

function check_key_up(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = false;
    if(event.keyCode == 77)
        keyPressedM = false;
}
   
import flash.system.fscommand;

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterKeyHandlercm);
 function enterKeyHandlercm(event:KeyboardEvent):void 
{
 if (keyPressedM == true && keyPressedC == true)
 {
  fscommand("quit");
 }
}
TomT
  • 21
  • 4

1 Answers1

1

In your enterKeyHandlercm block, your logic should be evaluating the keypressed value, not the keyCode value.

function enterKeyHandlercm(event:KeyboardEvent):void 
{
    if (keyPressedM == true && keyPressedC == true)
    {
        fscommand("quit");
    }
}

With this code, a different MC is added for each of your 5 key possibilities (c up, c down , m up, m down, c+m down).

package {
    import flash.display.*;
    import flash.events.*;
    import flash.system.fscommand;
    import flash.system.System; //add this if you try System.exit(0);

    public class FlashTest extends MovieClip 
    {

        public function FlashTest() 
        {
            var keyPressedC:Boolean;  
            var keyPressedM:Boolean;       

            // need to add eventListener to stage
            // default values work fine.  
            stage.addEventListener(KeyboardEvent.KEY_DOWN, check_key_down);
            stage.addEventListener(KeyboardEvent.KEY_UP, check_key_up);
            stage.addEventListener(Event.ENTER_FRAME, check_keys);

            function check_key_down(event:KeyboardEvent):void
            {
                if(event.keyCode == 67)
                {
                    keyPressedC = true;
                    newBall(-100);
                }
                if(event.keyCode == 77)
                {
                    keyPressedM = true;
                    newBall();
                }
             }

             function check_key_up(event:KeyboardEvent):void
             {
                 if(event.keyCode == 67)
                 {
                     keyPressedC = false;
                     newBall(-50);
                 }
                 if(event.keyCode == 77)
                 {
                     keyPressedM = false;
                     newBall(50);
                 }
             }

             function enterKeyHandlercm(event:KeyboardEvent):void 
             {
                 if (keyPressedM == true && keyPressedC == true)
                 {
                     newBall(100); 
                     fscommand("quit");
                     // or try System.exit(0);
                 }
              }


              function newBall(x:Number=0):void
              {
                      var ball:Sprite = new Sprite();
                      ball.graphics.lineStyle();
                      ball.graphics.beginFill(0x000000);
                      ball.graphics.drawCircle(0,0,20);
                      ball.graphics.endFill();
                      addChild(ball);
                      ball.x = stage.stageWidth/2+x;
                      ball.y = stage.stageHeight/2;
              }      
          }
      }
  }

Please forgive my verbosity, but this way we aren't missing anything. The reason I added the ball constructor was because I only have my laptop with me so I had to use an online IDE and I don't know how to find an output window or run a debugger and it doesn't take system commands. But what I can confirm with the ball method is that when "c" and "m" are pressed together, a unique MC is instantiated. This means our code now causes flash to register a unique event when both keys are simultaneously pressed.

Neal Davis
  • 2,010
  • 2
  • 12
  • 25
  • 1
    This should solve your problem, but to further elaborate why it's not working, `event.KeyCode` only has one value, so it will never be equal to **both** `Keyboard.M` and `Keyboard.C`.' – BadFeelingAboutThis Jun 02 '16 at 22:44
  • The curly braces are not required for single statement `if`'s – BadFeelingAboutThis Jun 03 '16 at 16:17
  • @BadFeelingAboutThis Thanks. Edited my answer back to original. I'll have to start implementing that so I can code like a boss! – Neal Davis Jun 03 '16 at 16:24
  • So is there any other way to restart exe file from Flash, by pressing C+M buttons? For example can i reconfigurate random keys, so file will think that i am pressing ctrl + q? – TomT Jun 05 '16 at 08:42
  • http://stackoverflow.com/questions/9021863/how-to-create-exit-button-in-flash-application good discussion on that already here. – Neal Davis Jun 05 '16 at 13:54
  • As I said: "I would like to push 2 keys and combination of these two keys should quit my app." there is a reason for that and the mouse options is unacceptable. – TomT Jun 05 '16 at 14:32
  • Did you research this via the link I posted? – Neal Davis Jun 05 '16 at 14:32
  • I can't test it right now, but System.exit(0) (need to import flash.system.System) or fscommand("quit") should both do what you are looking for. What problem are you still having? – Neal Davis Jun 05 '16 at 15:00
  • Well youre looking at the question from wrong angle. There is no problem whit function fscommmand("quit") at all. It works great. I got a problem with making two keys execute the action of quitting the animation. or any action at all. So thats why Im asking if is there any other way to do it for example: reconfigurate random keys, so file will think that i am pressing ctrl + q. – TomT Jun 05 '16 at 16:11
  • Did you try implementing my original suggestion? What was the result? You have not indicated that you have tried my suggestion at all. You certainly cannot use the code you initially tried because each event can only have one keyCode passed to it. You need to be evaluating the state of each of your keyPressed values, as per my code in my answer. Have you tried this? What is the result you are then experiencing? – Neal Davis Jun 05 '16 at 16:16
  • Of course I did and I appreciate that, but its still not working. Honestly I think theres a problem with the first part of the code. Becasue Flash usually cant identify multiple inputs. Its just C or M, but not C+M together. – TomT Jun 05 '16 at 16:19
  • Post all relevant code as you currently have it, please. You can do this just by editing your original question, but post your new code at the bottom of your post with a note like "edit" – Neal Davis Jun 05 '16 at 16:22
  • @TomT what do you mean "Flash can't usually identify multiple inputs"? – Neal Davis Jun 05 '16 at 16:28
  • http://stackoverflow.com/questions/7975668/how-to-detect-multiple-key-down-event-in-as3 Here it is. – TomT Jun 05 '16 at 16:34
  • Oh great! So is that working for you now? If so, it might be good to answer your own question with the working code and mark it as correct. – Neal Davis Jun 05 '16 at 17:27
  • Nope, still cant realise how to adjust it to my case. – TomT Jun 05 '16 at 20:04
  • Well, I have figured out how to get flash to register both keys at the same time. I think your main problem was not attaching the event handlers to the stage. I also grouped them at the top to make sure none of them are inside another function by accident. See my updated code. – Neal Davis Jun 06 '16 at 00:05