Conveniently, it appears that the extra events generated by key repeat

are all KEY_DOWN events.
So holding down a key in flash generates: DOWN, DOWN, DOWN, ... UP.
In Java, we get DOWN, UP, DOWN, UP ...

Made a little helper class that can be used to block the extra DOWN events.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@195 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Ray Greenwell
2007-04-12 23:49:29 +00:00
parent 54fd9e0df4
commit a8a3295f26
@@ -0,0 +1,58 @@
package com.threerings.flash {
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.KeyboardEvent;
import flash.utils.Dictionary;
import flash.utils.getTimer; // function import
/**
* A very simple class that adapts the KeyboardEvents generated by some source by discarding
* the extra KEY_DOWN events generated while a key is held down.
*/
public class KeyRepeatBlocker extends EventDispatcher
{
/**
* Create a KeyRepeatBlocker that will be blocking key repeat events from
* the specified source.
*/
public function KeyRepeatBlocker (source :IEventDispatcher)
{
_source = source;
_source.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
_source.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
}
/**
* Dispose of this KeyRepeatBlocker.
*/
public function shutdown () :void
{
_source.removeEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
_source.removeEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
}
protected function handleKeyDown (event :KeyboardEvent) :void
{
if (!_down[event.keyCode]) {
_down[event.keyCode] = true;
dispatchEvent(event);
}
}
protected function handleKeyUp (event :KeyboardEvent) :void
{
delete _down[event.keyCode];
dispatchEvent(event);
}
/** Our source. */
protected var _source :IEventDispatcher;
/** Tracks whether a key is currently being held down. */
protected var _down :Dictionary = new Dictionary();
}
}