So I know a while ago I gave you guys a MultiKey class for detecting when key combinations have been pressed. I started using that and found that… it sucked, bad… I spent the last hour putting together a much nicer class for detecting Key Combinations, and here it is…
Save as, KeyDetection.as
/* Created to allow us to listen for key combinations.
*
* @author Michael Avila
* @version 1.0
*/
class KeyDetection
{
// a list of all the key codes that have been pressed
private var keys_pressed : Array;
// a multi-dimensional list of all of our key combinations
private var key_combinations : Array;
// objects listening to this detection
private var listeners : Array;
/* Constructor */
public function KeyDetection ()
{
keys_pressed = new Array ();
key_combinations = new Array ();
listeners = new Array ();
// allow this object to listen for events from the key object
Key.addListener (this);
}
/* Registers an object to listen for events from the KeyDetection class
*
* @param The object that will listen for the events
*/
public function addListener (listener : Object) : Void
{
for (var i : Number = 0; i < listeners.length; i ++)
{
if (listeners * == listener) return;
}
listeners.push (listener);
}
/* Unregisters an object that is listening for events from the KeyDetection class
*
* @param The object you wish to remove from the listeners list
*/
public function removeListener (listener : Object) : Void
{
for (var i : Number = 0; i < listeners.length; i ++)
{
if (listeners * == listener) listeners.splice (i, 1);
}
}
/* Adds a key combination to listen for
*
* @param The name you are giving this combination. Note that this is how you will identify which combination
* has been pressed.
* ...
* @param The key codes that you are part of this combination. Note that they will need to be pressed in the order
* that you list them in order for the combination to be fire successfully.
*
* @usage <pre>var key_detector = new KeyDetection();
* key_detector.addCombination("undo", Key.CONTROL, 90);
* </pre>
*
*/
public function addCombination (name : String, keyCode1 : Number, keyCode2 : Number) : Void
{
key_combinations.push (arguments);
}
// invokes the onKeyCombination event on all listeners
private function invokeOnKeyCombination (combo_name : String ) : Void
{
for (var i : Number = 0; i < listeners.length; i ++)
{
listeners *.onKeyCombination (combo_name);
}
}
private function onKeyDown ()
{
var key : Number = Key.getCode ();
cleanKeysPressed ();
if (key != keys_pressed [keys_pressed.length - 1])
{
keys_pressed.push (key);
}
checkCombinations ();
}
private function checkCombinations ()
{
for (var j : Number = 0; j < key_combinations.length; j ++)
{
for (var i : Number = 0; i < keys_pressed.length; i ++)
{
if (keys_pressed * == key_combinations [j][i + 1])
{
if (i == key_combinations [j].length - 2)
{
invokeOnKeyCombination (key_combinations [j][0]);
return;
}
} else
{
break;
}
}
}
}
private function cleanKeysPressed ()
{
for (var i : Number = 0; i < keys_pressed.length; i ++)
{
if ( ! Key.isDown (keys_pressed *))
{
keys_pressed.splice (i, (keys_pressed.length - i));
}
}
}
}
Here is an example of how you’d use it in your fla…
_root.createTextField("message_txt", 100, 0, 0, 550, 400);
var keyDet = new KeyDetection();
keyDet.addCombination("undo", Key.CONTROL, 90);
keyDet.addCombination("redo", Key.CONTROL, 89);
myObj = new Object();
myObj.onKeyCombination = function(name:String)
{
switch (name)
{
case "undo":
message_txt.text = "Undo Combination Pressed";
break;
case "redo":
message_txt.text = "Redo Combination Pressed";
break;
}
}
keyDet.addListener(myObj);
WARNING: In order for this example to work you have to view it either in a browser or a stand alone swf player, because the Flash IDE takes over the CONTROL key so once it’s pressed you don’t hear from any other keys.
Anyway this is an extremely easy way to detect key combinations… there’s also documentation for this class found here…
www.nowtheworld.com/ASDocs/KeyDetection
This has been bugging me for a while, so I’d like to get some feedback on it, take care guys.
_Michael