# Multiple Key detection issue

**URL:** <https://forum.kirupa.com/t/multiple-key-detection-issue/205162>\
**Category:** flash\
**Created:** [October 26, 2006, 6:51pm UTC](https://forum.kirupa.com/t/multiple-key-detection-issue/205162 "2006-10-26T18:51:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbaek86](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@rbaek86](https://forum.kirupa.com/u/rbaek86)\
**Post date:** [October 26, 2006, 6:51pm UTC](https://forum.kirupa.com/t/multiple-key-detection-issue/205162/1 "2006-10-26T18:51:14Z")

</div>

Hi,  
New here, have a little problem

I’m using Michael Avila’s muti key detection class

```auto
 /* 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 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 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));
   }
  }
 }
}

```

in my movie I am trying to detect a sequential key press from “k” to “d”  
and then a sound plays.

```auto
var combo_kd:KeyDetection = new KeyDetection();
combo_kd.addCombination("k_d",75,68);
var listener_obj:Object = new Object();
var someListener = new Object();
listener_obj.onKeyCombination = function()
{
 mcroar.gotoAndPlay(2);
};
combo_kd.addListener(listener_obj);

```

The issue I need help with is that it keeps tracing  
as “k” and “d” is held down  
whereas I would like it to trace once and then  
trigger again after “k” and “d” are pressed again.

Thanks for the help!
