scaffold.libs_as.feathers.utils.keyboard.KeyToTrigger.as Maven / Gradle / Ivy
/*
Feathers
Copyright 2012-2015 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.utils.keyboard
{
import feathers.core.IFocusDisplayObject;
import feathers.events.FeathersEventType;
import flash.ui.Keyboard;
import starling.events.Event;
import starling.events.KeyboardEvent;
/**
* Dispatches Event.TRIGGERED
from the target when a key is
* pressed and released and the target has focus. Conveniently handles all
* KeyboardEvent
listeners automatically.
*
* In the following example, a custom item renderer will be triggered
* when a key is pressed and released:
*
*
* public class CustomComponent extends FeathersControl implements IFocusDisplayObject
* {
* public function CustomComponent()
* {
* super();
* this._keyToTrigger = new KeyToTrigger(this);
* this._keyToTrigger.keyCode = Keyboard.SPACE;
* }
*
* private var _keyToTrigger:KeyToTrigger;
* // ...
*
* Note: When combined with a KeyToSelect
instance, the
* KeyToTrigger
instance should be created first because
* Event.TRIGGERED
should be dispatched before
* Event.CHANGE
.
*
* @see http://doc.starling-framework.org/current/starling/events/Event.html#TRIGGERED starling.events.Event.TRIGGERED
* @see feathers.utils.touch.KeyToSelect
* @see feathers.utils.touch.TapToTrigger
*/
public class KeyToTrigger
{
/**
* Constructor.
*/
public function KeyToTrigger(target:IFocusDisplayObject = null)
{
this.target = target;
}
/**
* @private
*/
protected var _target:IFocusDisplayObject;
/**
* The target component that should be selected when a key is pressed.
*/
public function get target():IFocusDisplayObject
{
return this._target;
}
/**
* @private
*/
public function set target(value:IFocusDisplayObject):void
{
if(this._target === value)
{
return;
}
if(this._target)
{
this._target.removeEventListener(FeathersEventType.FOCUS_IN, target_focusInHandler);
this._target.removeEventListener(FeathersEventType.FOCUS_OUT, target_focusOutHandler);
this._target.removeEventListener(Event.REMOVED_FROM_STAGE, target_removedFromStageHandler);
}
this._target = value;
if(this._target)
{
this._target.addEventListener(FeathersEventType.FOCUS_IN, target_focusInHandler);
this._target.addEventListener(FeathersEventType.FOCUS_OUT, target_focusOutHandler);
this._target.addEventListener(Event.REMOVED_FROM_STAGE, target_removedFromStageHandler);
}
}
/**
* @private
*/
protected var _keyCode:uint = Keyboard.SPACE;
/**
* The key that will trigger the target, when pressed.
*
* @default flash.ui.Keyboard.SPACE
*/
public function get keyCode():uint
{
return this._keyCode;
}
/**
* @private
*/
public function set keyCode(value:uint):void
{
this._keyCode = value;
}
/**
* @private
*/
protected var _cancelKeyCode:uint = Keyboard.ESCAPE;
/**
* The key that will cancel the trigger if the key is down.
*
* @default flash.ui.Keyboard.ESCAPE
*/
public function get cancelKeyCode():uint
{
return this._cancelKeyCode;
}
/**
* @private
*/
public function set cancelKeyCode(value:uint):void
{
this._cancelKeyCode = value;
}
/**
* @private
*/
protected var _isEnabled:Boolean = true;
/**
* May be set to false
to disable selection temporarily
* until set back to true
.
*/
public function get isEnabled():Boolean
{
return this._isEnabled;
}
/**
* @private
*/
public function set isEnabled(value:Boolean):void
{
this._isEnabled = value;
}
/**
* @private
*/
protected function target_focusInHandler(event:Event):void
{
this._target.stage.addEventListener(KeyboardEvent.KEY_DOWN, stage_keyDownHandler);
}
/**
* @private
*/
protected function target_focusOutHandler(event:Event):void
{
this._target.stage.removeEventListener(KeyboardEvent.KEY_DOWN, stage_keyDownHandler);
this._target.stage.removeEventListener(KeyboardEvent.KEY_UP, stage_keyUpHandler);
}
/**
* @private
*/
protected function target_removedFromStageHandler(event:Event):void
{
this._target.stage.removeEventListener(KeyboardEvent.KEY_DOWN, stage_keyDownHandler);
this._target.stage.removeEventListener(KeyboardEvent.KEY_UP, stage_keyUpHandler);
}
/**
* @private
*/
protected function stage_keyDownHandler(event:KeyboardEvent):void
{
if(!this._isEnabled)
{
return;
}
if(event.keyCode === this._cancelKeyCode)
{
this._target.stage.removeEventListener(KeyboardEvent.KEY_UP, stage_keyUpHandler);
}
else if(event.keyCode === this._keyCode)
{
this._target.stage.addEventListener(KeyboardEvent.KEY_UP, stage_keyUpHandler);
}
}
/**
* @private
*/
protected function stage_keyUpHandler(event:KeyboardEvent):void
{
if(!this._isEnabled)
{
return;
}
if(event.keyCode !== this._keyCode)
{
return;
}
this._target.dispatchEventWith(Event.TRIGGERED);
this._target.stage.removeEventListener(KeyboardEvent.KEY_UP, stage_keyUpHandler);
}
}
}