import mx.utils.Delegate; import mx.events.EventDispatcher; /** * @author e.bonnet */ class com.emmanuelbonnet.tools.InactiveDetect { private var _nTime:Number; private var _bListenActive:Boolean; private var _bDetect:Boolean; private var _eMouse:Object; private var _eKey:Object; public var dispatchEvent:Function; public var addEventListener:Function; public var removeEventListener:Function; public var onInactive:Function; public var onActive:Function; private var _nInterval:Number; function InactiveDetect(pTime:Number, pListenActive:Boolean) { _nTime = pTime*1000; _bListenActive = pListenActive; EventDispatcher.initialize(this); addEventListener("onInactive", this); addEventListener("onActive", this); startListen(); } public function startListen():Void { _eMouse = new Object(); _eKey = new Object(); _eMouse.onMouseMove = _eMouse.onMouseDown = _eMouse.onMouseUp = _eMouse.onMouseWheel = Delegate.create(this, _action); _eKey.onKeyDown = _eKey.onKeyUp = Delegate.create(this, _action); Mouse.addListener(_eMouse); Key.addListener(_eKey); _action(); } public function stopListen():Void { clearInterval(_nInterval); Mouse.removeListener(_eMouse); Key.removeListener(_eKey); delete _eMouse; delete _eKey; } private function _action():Void { clearInterval(_nInterval); _nInterval = setInterval(this, '_onInactive', _nTime); if(_bListenActive && _bDetect) { dispatchEvent({type: "onActive", target: this}); _bDetect = false; } } private function _onInactive():Void { clearInterval(_nInterval); dispatchEvent({type: "onInactive", target: this}); if(!_bListenActive) stopListen(); _bDetect = true; } }