Archive for the ‘Opensource’ Category

Multiple–Key Combination Detection

I have the need to detect multiple-key combinations from the user in the delicious:designer. While porting the designer to ActionScript 3 I decided to refine this behavior and came across this post with an ActionScript 2 class for detecting multi-key combos. I ported this class to AS3, borrowed this other class to replace the missing Key.isDown() function in AS3, and wrote a custom KeyComboEvent event class to create a rather nice multi-key detection system that will allow for far greater flexibility than the current (build 38) key detector.This is all that is needed to create and listen for multiple-key combinations in ActionScript 3

import com.Keyboard.*;

import flash.events.*;// Key is used so we can test for a key being down, like we used to do in AS2Key.initialize(stage);

// Create your key detector and set up a few combinations to test for

var keyDet = new KeyDetection(stage);keyDet.addCombination(“undo”, Keyboard.CONTROL, 90);

// Control + ZkeyDet.addCombination(“redo”, Keyboard.CONTROL, 89);

// Control + YkeyDet.addCombination(“three keys”, Keyboard.CONTROL, Keyboard.SHIFT, 89);

// Control + Shift + Y

// Add a listener so we can respond to key combinations when they are pressed

keyDet.addEventListener(KeyComboEvent.KEY_COMBINATION, keyCombo);

function keyCombo(event:KeyComboEvent):void

{

    switch (event.label)

    {

        case “undo”:

 		trace(“Undo Combination Pressed”);

 		textArea.appendText(“Undo Combination Pressedn”);

 		break;

 	case “redo”:

 		trace(“Redo Combination Pressed”);

 		textArea.appendText(“Redo Combination Pressedn”);

 		break;

 	case “three keys”:

 		trace(“Our three-key Combination was Pressed”);

 		textArea.appendText(“Our three-key Combination Pressedn”);

 		break;

 	}

 }

}

Click here to download the Flash 9 sample with source code, and click here to view the package documentation.

ActionScript 2 double interval

Within the delicious:suite I often have the need of calling a function at an interval. But I need a two step timer to call that function at a slower interval the first few times, and then call it at a faster interval.

I wrote a custom class to mimic the Flash ActionScript 2 built in setInterval() and clearInterval() functions, but with the addition of this two-step timer. The sample swf demoed here shows my stepper that increments a number up and down. When you press and hold either arrow button the number increments slowly for the first 10, and then increments faster until you release the button. Another example of this class in action is the nudge function in the delicious:designer — the user can nudge art objects using the arrow keys. The first nudge occurs at a long interval, and then, if the user continues holding the key down, the nudge continues at a faster interval.Here is sample ActionScript 2 code showing how simple it can be to create a double interval function call. This example will call the function myFunction 10 times every 200 milliseconds. Then it will call the function myFunction every 50 milliseconds until the user clears it.

import mx.utils.Delegate;
import delicious.doubleInterval;
intervalNumber = doubleInterval.setDoubleInterval( Delegate.create( this, myFunction ), 200, 10, 50);


For more advanced uses you can also pass parameters to the function called on the interval. The class can be downloaded here, and the documentation can be viewed here.