EladElrom.com

Deep Dive Into Technology

Small utility class to handle Flex, ActionScript keyboard operations such as Copy, Paste, Cut, Undo, Redo etc

Here’s a quick small utility class that you can use as a starting point to build a keyboard tracker to handle Copy, Paste, Cut, Undo, Redo etc operations from the user.

package utils
{
	public final class KeyBoardCombination
	{
		public static const UNDO:int  = 0;
		public static const REDO:int  = 1;
		public static const CUT:int   = 2;
		public static const PASTE:int = 3;
		public static const COPY:int  = 4;

		public static function  get getCombinationCollection():Vector.<array>
		{
			var combinationCollection:Vector.<array> = new Vector.<array>();
			combinationCollection.push( new Array( 17, 90 ) );
			combinationCollection.push( new Array( 17, 16, 90 ) );
			combinationCollection.push( new Array( 17, 88 ) );
			combinationCollection.push( new Array( 17, 86 ) );
			combinationCollection.push( new Array(17, 67) );

			return combinationCollection;
		}

		public static function checkKeyboardCombination(keyboardPressedCollection:Array):int
		{
			var retCombinationState:int = -1;
			var combinationLength:int;
			var confirmedKeys:int = 0;
			var combinationCollection:Vector.<array> = KeyBoardCombination.getCombinationCollection;

			combinationCollection.forEach(function callback(combination:Array, selectedState:int, combinationCollection:Vector.<array>):void{

				confirmedKeys = 0;

				combination.forEach( function callback(checkKey:int, index:int, array:Array):void {

					combinationLength = combination.length+1;

					keyboardPressedCollection.forEach(function callback(keyPressed:int, idx:int, arr:Array):void {

						if (keyPressed == checkKey)
							confirmedKeys++;
					});

					if (confirmedKeys == combinationLength-1)
						retCombinationState = selectedState;
				});
			});

			return retCombinationState;
		}
	}
}

Implementation looks like this:

private var keyboardCollectionPressed:Array = new Array();
			mainView.stage.addEventListener(KeyboardEvent.KEY_DOWN, function(event:KeyboardEvent):void {
				keyboardCollectionPressed.push( event.keyCode );
			} );
			mainView.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);

		private function onKeyboardUp(event:KeyboardEvent):void
		{
			var combination:int = KeyBoardCombination.checkKeyboardCombination( keyboardCollectionPressed );

			switch (combination)
			{
				case KeyBoardCombination.UNDO:
					trace("UNDO");
					break;
				case KeyBoardCombination.REDO:
					trace("REDO");
					break;
				case KeyBoardCombination.CUT:
					trace("CUT");
					break;
				case KeyBoardCombination.PASTE:
					trace("PASTE");
					break;
				case KeyBoardCombination.COPY:
					trace("COPY");
					break;
			}

			keyboardCollectionPressed = new Array();
		}

Flex Camp Wall Street conference

I can’t believe another year has passed and we are only few days away from the next edition of Flex Camp Wall Street.

Lots of great sessions and great speakers, including a good bit of AIR on Android,
new Flex 4.5 and AIR 2.5 features, reliable BlazeDS communication,

Flex and AIR application testing and performance tuning.
The prices are the same as last year: just $49 and lunch (Pizzas) is included within
the ticket price. We have a new and nicer venue as well! In addition, some nice giveaways to
be raffled at the event.

Go to www.flexcampwallstreet.com to register for the event and get more details.