Friday, November 7, 2008

Flex 3 - KeyBoard Events, Flash Player 10 vs Flash Player 9

In trying to work on one of the requirements which requires us to handle Shortcut keys (example: Cntrl+1, Cntrl+2 etc), Using Flex Keyboard events, I happened to hit upon this same on the live docs. Flex 3 Docs Sample

Initially i thought It broken in Flash player 10 and is working fine in flash player 9. Place your cursor in the Text area below and press (cntrl+1) and see what you get. (Keycode for Cntrl key = 17). This swf is generated using flash player 10

See this SWF (generated using flash player 9) which works almost the same way. Place your cursor in the Text area below and press (cntrl+1) and see what you get. If you press the keys 'cntrl' and '1' fast and release them fast, only cntrl event is fired and cntrl+1 event is not fired. Keep doing it fast, and at one point you will see both the keys start to respond, from here on everything keeps to be working. Open the url in a new browser window to retry this scenario (looks like key-up event is not getting fired initially when we press and release the keys faster).

And here the the same code that made it happen..

        private function initApp():void {
            application.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
            my_vbox.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
            my_textinput.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

            // Set the focus somewhere inside the application.
            my_textinput.setFocus();
        }

        private function keyHandler(event:KeyboardEvent):void {
            ta1.text += event.target + "(" + event.currentTarget + "): " + 
                event.keyCode + "/" + event.charCode + "\n";
        }




    
        
    

    



Monday, November 3, 2008

Getter / Setter Generator in Adobe Flex

While working on my current project using the Pure MVC framework, which involes writing a lot of VO (value objects) that are a set of getter, setter accessor methods, using Flex Builder 3 is really tough. Thanks to these guys who created the Eclipse Monkey Plugin - Wow amazing. This saves a lot of time atleast in writing actual code instead of just getters/setters. Just to give you a headsup, this feature was implemented as a part of Flex Builder 4 feature by one of the developers at US, while i was at adobe bangalore about 6 months ago. I am sure that will help a lot of developers in not reiterating a routine boring work of hand-coding it.

Sunday, November 2, 2008

Verify if Object contains a proprety defined or not

In Adobe flex, you want to check if a property is defined inside an object, its pretty easy.
  /**
  * Test if the object obj contains a property named val
  *
  **/
  private function propertyTest(obj:Object):void
  {
     if(obj.hasOwnProperty("val"))
     {
       Alert.show('Has property named val defined and its value = ' + obj.val);
     }
     else
     {
       Alert.show('Property val is not defined in the object - obj');
     }
  }
  
  /**
  * Test method
  *
  **/
  private var testProperty():void
  {
     var val:String = "Test String";
     var obj:Object = new Object();
     obj.val = val;
 
     //test method call
     propertyTest(obj);
  }