3 Replies Latest reply on Jul 25, 2010 1:54 PM by BBCLX

    Keyboard Shortcut for Buttons

    BBCLX Level 1

      Hy there

       

      I have  a Toolbar with buttons to which I would like to add shortcuts, depending on the OS.

       

      The "New Action" should be CMD + N on the Mac and CTRL + N on Windows.

      The "Send Action" should be CMD + SHIFT + D on the Mac and ALT + SHIFT + D on Windows respectively.

       

      So what I now do is this:

       


      The method, which gets called on KeyDown:

      public function onGlobalKeyDown(e:KeyboardEvent):void {
      
          if (AirUtil.isSendShortCut(e)) {
              dispatchEvent(new Event('onSendShortcut'))
          }
      
          if (AirUtil.isNewShortcut(e)) {
              dispatchEvent(new Event('onNewShortcut'))
          }
      }
      

       

      My Utility mehod:

       

      public static function isSendShortCut(e:KeyboardEvent):Boolean {
          
          if (!e.controlKey && e.shiftKey && e.keyCode == Keyboard.D) {
      
              if (isMac() && !e.altKey && e.commandKey) {
                  return true;
              }
      
              if (!isMac() && e.altKey && !e.commandKey) {
                  return true;
              }
          }
          return false;
      }
      
      public static function isNewShortcut(e:KeyboardEvent):Boolean {
          if (!e.altKey && e.keyCode == Keyboard.N) {
      
              if (isMac() && !e.controlKey && e.commandKey) {
                  return true;
              }
              else if (!isMac() && e.controlKey && !e.commandKey) {
                  return true;
              }
          }
          return false;
      }
      
      public static function isMac():Boolean {
          return Capabilities.os.indexOf('Mac') != -1;
      }
      

       

       

      This seems to be a bit dodgy and imho too much code and I wonder if there is  a cleaner way to do this.

       

      What's more is, that when I hit CTRL + N on Windows the action kicks in correctly, but after the action is performed, a special not-known character (displayed as a box) is inserted in the UI. On OSX the function works well. I have tried to switch to onKeyUp which caused the OSX version to not work anymore and the windows version still behaved the same (including the unwanted box).

       

      Help or suggestions would be appreciated!

       

       

      UPDATE: When I use the send action ALT + SHIFT + D on Windows a 'D' character is inserted in the current textfield and then the message is sent. This is equivalent to the box on CTRL + N.

        • 1. Re: Keyboard Shortcut for Buttons
          BBCLX Level 1

          I've been waiting and (mostly) crying for two months now, has anyone a suggestion for this problem?

           

          UPDATE: Almost another three months and still no reply. All the required information should be in my first post, if not please tell me what wrong with it...

          • 2. Re: Keyboard Shortcut for Buttons
            SilentChris Level 1

            As far as the weird box character is concerned, I think you should be able to call e.preventDefault() and e.stopImmediatePropagation() in the keyDown handler to prevent the event from inserting a character in the TextArea.  With regard to the flow in general, I think your setup is ok.  In our application, we often allow both ctrl and cmd shortcuts for keys do the same thing on mac (e.g. cmd-n and ctrl-n will be the same action).  Then on windows, since there is no command key, ctrl is the only option.

             

            Personally, I find it cleaner to just put all the key checking / function calling in the keyDown handler and just separate the functionality for the actions (rather than using events and helper functions like you've done).  I don't think either way is particularly better or worse though.

            1 person found this helpful
            • 3. Re: Keyboard Shortcut for Buttons
              BBCLX Level 1

              

               

              I pasted the character here and it seems to be 00-7F in unicode. I have tried the following methods on the keyDown, none of them worked:

               

              event.stopPropagation

              event.stopImmediatePropagation

              event.preventDefault