From a8a3295f268f7180d63f43aa421214e8ae182626 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 12 Apr 2007 23:49:29 +0000 Subject: [PATCH] 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 --- .../com/threerings/flash/KeyRepeatBlocker.as | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/as/com/threerings/flash/KeyRepeatBlocker.as diff --git a/src/as/com/threerings/flash/KeyRepeatBlocker.as b/src/as/com/threerings/flash/KeyRepeatBlocker.as new file mode 100644 index 00000000..bdc211ba --- /dev/null +++ b/src/as/com/threerings/flash/KeyRepeatBlocker.as @@ -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(); +} +}