5 Replies Latest reply on Jan 6, 2010 5:30 AM by menik8

    Overwrite default NativeMenu KeyEquivalents???

    menik8

      Hello guys,

       

      maybe I'm just thinking to complicated. However i wonder if it is possible to fire a function in an AIR Window if i hit CMD-C (or CTRL-C on windows).

       

      For instance if you do a normal simple rectangle on the stage in Flash and you publish it as an AIR App ADL starts, right! if you look to the menubar, you see under "EDIT" the default shortcuts like CMD-C for copy, CMD-V for paste or CMD-X to cut text.

       

      I'm currently programming an app where i don't have any textfields where you can copy stuff, so the default Shortcuts in the EDIT-menu for copying or pasting aren't neceserry. However i want to use them for calling a normal functon.

       

      For instance if my AIR app is open and i hit CMD-C i want to tween something, just a function call.

       

      var menu:NativeMenu = new NativeMenu();
          var
      tweenMyObjectCommand: NativeMenuItem = menu.addItem(new NativeMenuItem("Call the Tweening Function")); //Text shown in menu
         
      tweenMyObjectCommand.keyEquivalent = "C"; //Command Shift C, i want command c
         
      tweenMyObjectCommand.addEventListener(Event.SELECT, myTweenFunction); //call the function

       

      Is this possible? Thank you for your help!

        • 1. Re: Overwrite default NativeMenu KeyEquivalents???
          menik8 Level 1

          No one an idea? Please help me guys!

          • 2. Re: Overwrite default NativeMenu KeyEquivalents???
            heavyboots Level 2

            And lower-casing the keyEquivalent to 'c' from 'C' doesn't work? I would think it would but haven't specifically tried it.

             

            Menu item equivalents will override KeyboardEvents, but if you remove the .keyEquivalent you might also do it this way:

             

            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            
            function keyPressed( e:KeyboardEvent ):void {
                 switch( e.keyCode ) {
                      case Keyboard.C:
                           // check if Control Key down too
                           if( e.ctrlKey ) {
                                    tweenMyObjectCommand();
                           }
                      break;
                      // and so forth
                 }
            }
            


            That's how I started out doing keyboard commands until I added my Native Menu.

            • 3. Re: Overwrite default NativeMenu KeyEquivalents???
              menik8 Level 1

              Well, that's exactly what i mean! Of course the ctrlKey is working, becuase there is no default NativeMenu Item on it.

              I'm on a Mac and in this case i don't want to use the ctrlKey BUT the commandKey (with the Apple Symbol)!

               

               

              if( e.ctrlKey ) {

              This is working! (CTRL -C on my mac is firing, CommandKey isn't firing!)

               

              if( e.controlKey ) {

              Is working as well! (CTRL -C on my mac is firing, CommandKey isn't firing!)

               

              HOWEVER.... if I'm trying to use the CommandKey...

              if( e.commandKey ) {

              ITS NOT WORKING, because (i guess) command C is already taken by the default COPY Command.

               

              Got my problem?

              • 4. Re: Overwrite default NativeMenu KeyEquivalents???
                heavyboots Level 2

                Ahhh, your issue is that you're not defining the native menu bar for the app to be your menu so you're just getting the defaults. Also, I think you're missing a submenu to put the command in.

                 

                Try something more like this:

                 

                 

                var menu:NativeMenu = new NativeMenu();
                var fileMenu:NativeMenu = new NativeMenu;
                
                var tweenMyObjectCommand:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("Call the Tweening Function")); //Text shown in menu
                tweenMyObjectCommand.keyEquivalent = "c"; //Should work now
                tweenMyObjectCommand.addEventListener(Event.SELECT, myTweenFunction); //call the function
                
                menu.addSubmenu(fileMenu, "File");
                
                if( NativeApplication.supportsMenu ) { 
                     // Mac global menubar
                     NativeApplication.nativeApplication.menu = menu; 
                } else if (NativeWindow.supportsMenu) { 
                     // Windoze/Linux window menubar
                     stage.nativeWindow.menu = menu;
                     // Note that we apparently have to increase stage height to accomodate the menu bar pushing everything down on the stage when it draws into the window
                     stage.stageHeight += 21;
                }
                
                
                
                     
                function myTweenFunction(e:Event):void {
                     trace("Made it!");
                }
                

                 

                 

                EDIT: Actually, I don't think my submenu setup is 100% right, but it gets something onscreen at least. Check the NativeMenu section of the manual. They have some pretty good examples there actually.

                • 5. Re: Overwrite default NativeMenu KeyEquivalents???
                  menik8 Level 1

                  Oh, yeah! Thank you very much!