Key

Kind of class:public class
Package:org.casalib.ui
Inherits from:RemovableEventDispatcher < EventDispatcher
Version:09/06/09
Author:Aaron Clinger
Classpath:org.casalib.ui.Key
File last modified:Sunday, 06 September 2009, 09:08:21
Key class that simplifies listening to global key strokes and adds additional keyboard events. Key enables you to receive events when multiple keys are held/released and when a sequence of keys are pressed.
Usage note:
Example:
  • package {
        import flash.events.KeyboardEvent;
        import org.casalib.display.CasaMovieClip;
        import org.casalib.events.KeyComboEvent;
        import org.casalib.ui.Key;
        import org.casalib.ui.KeyCombo;
        import org.casalib.util.StageReference;
    
    
        public class MyExample extends CasaMovieClip {
            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
Class methods
Instance methods

Class methods

getInstance

public static function getInstance (
) : Key

Returns:
  • The Key instance.
Usage note:

Instance methods

addKeyCombo

public function addKeyCombo (
keyCombo:KeyCombo) : void

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.

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; otherwise false.

removeKeyCombo

public function removeKeyCombo (
keyCombo:KeyCombo) : void

Removes a key combination from triggering a KeyComboEvent.
Parameters:
keyCombo:
A defined KeyCombo object.