Key
Kind of class: | public class |
---|---|
Package: | org.casalib.ui |
Inherits from: | RemovableEventDispatcher < EventDispatcher |
Version: | 10/26/08 |
Author: | Aaron Clinger |
Classpath: | org.casalib.ui.Key |
File last modified: | Wednesday, 31 December 2008, 20:18:28 |
Key class that simplifies listening to global key strokes and adds additional keyboard events. Key enables you to recieve events when multiple keys are held/released and when a sequence of keys are pressed.
Usage note:
- You must first initialize StageReference before using this class.
Example:
-
package { import flash.events.KeyboardEvent; import flash.display.MovieClip; import org.casalib.ui.Key; import org.casalib.ui.KeyCombo; import org.casalib.events.KeyComboEvent; import org.casalib.util.StageReference; public class MyExample extends MovieClip { protected var _asdfCombo:KeyCombo; protected var _casaCombo:KeyCombo; protected var _key:Key; public function MyExample() { super(); StageReference.setStage(this.stage); this._key = Key.getInstance(); this._asdfCombo = new KeyCombo(new Array(65, 83, 68, 70)); this._key.addKeyCombo(this._asdfCombo); this._casaCombo = new KeyCombo(new Array(67, 65, 83, 65)); this._key.addKeyCombo(this._casaCombo); this._key.addEventListener(KeyComboEvent.DOWN, this._onComboDown); this._key.addEventListener(KeyComboEvent.RELEASE, this._onComboRelease); this._key.addEventListener(KeyComboEvent.SEQUENCE, this._onComboTyped); this._key.addEventListener(KeyboardEvent.KEY_DOWN, this._onKeyPressed); this._key.addEventListener(KeyboardEvent.KEY_UP, this._onKeyReleased); } protected function _onComboDown(e:KeyComboEvent):void { if (this._asdfCombo.equals(e.keyCombo)) { trace("User is holding down keys a-s-d-f."); } } protected function _onComboRelease(e:KeyComboEvent):void { if (this._asdfCombo.equals(e.keyCombo)) { trace("User no longer holding down keys a-s-d-f."); } } protected function _onComboTyped(e:KeyComboEvent):void { if (this._casaCombo.equals(e.keyCombo)) { trace("User typed casa."); } } protected function _onKeyPressed(e:KeyboardEvent):void { trace("User pressed key with code: " + e.keyCode + "."); } protected function _onKeyReleased(e:KeyboardEvent):void { trace("User released key with code: " + e.keyCode + "."); } } }
Events broadcasted to listeners:
- KeyboardEvent with type:
KEY_DOWN
- Dispatched when the user presses a key. - KeyboardEvent with type:
KEY_UP
- Dispatched when the user releases a key. - KeyComboEvent with type:
RELEASE
- Dispatched whens all keys in an added held/released are no longer being held together at once. - KeyComboEvent with type:
SEQUENCE
- Dispatched when all keys in an added held/released are typed in order. - KeyComboEvent with type:
DOWN
- Dispatched when all keys in an added held/released are held down together at once.
Summary
Instance properties
Instance properties inherited from RemovableEventDispatcher
Class methods
Instance methods
- isDown (keyCode:uint) : Boolean
- Determines if is key is down.
- addKeyCombo (keyCombo:KeyCombo) : void
- Sets a key combination to trigger a KeyComboEvent.
- removeKeyCombo (keyCombo:KeyCombo) : void
- Removes a key combination from triggering a KeyComboEvent.
- destroy : void
Instance methods inherited from RemovableEventDispatcher
Class methods
getInstance
Returns:
- The Key instance.
Usage note:
- You must first initialize StageReference before using this class.
Instance methods
addKeyCombo
Sets a key combination to trigger a KeyComboEvent.
Parameters:
keyCombo:
A defined KeyCombo object.
destroy
override public function destroy (
) : void
Throws:
- Error if called. Cannot destroy a singleton.
Overrides:
isDown
public function isDown (
keyCode:uint) : Boolean
Determines if is key is down.
Parameters:
keyCode:
The key code value assigned to a specific key or a Keyboard class constant associated with the key.
Returns:
- Returns
true
if key is currently pressed; otherwisefalse
.
removeKeyCombo
Removes a key combination from triggering a KeyComboEvent.
Parameters:
keyCombo:
A defined KeyCombo object.