Saturday, October 20, 2012

Creating EasyKeyboard class: Part 2

Today we continue working on EasyKeyboard class and add the ability to listen to simple keyboard events.

By simple keyboard events I mean the built-in KEY_DOWN and KEY_UP events.

Go to EasyKeyboard.as and add a new array called listeners.

public var listeners:Array = [];

This array will hold objects of KeyListener class, which is a custom class that we will now create. Add a new file to the folder, call it KeyListener.as.

The code of the class is very simple. It is basically a holder of 6 public variables - keyCode, handlerD, handlerU, alt, ctrl and shift.

package com.kircode.EasyKeyboard 
{
/**
 * ...
 * @author Kirill Poletaev
 */
public class KeyListener 
{

public var keyCode:int;
public var handlerD:Function;
public var handlerU:Function;
public var alt:Boolean;
public var ctrl:Boolean;
public var shift:Boolean;

public function KeyListener(code:int, funcD:Function, funcU:Function, a:Boolean, c:Boolean, s:Boolean) 
{
keyCode = code;
handlerD = funcD;
handlerU = funcU;
alt = a;
ctrl = c;
shift = s;
}

}

}

Now add a new method in EasyKeyboard.as called addListener(), add 6 parameters to it that are similar to the properties of KeyListener. In the method itself, we take these parameters and apply them to a new KeyListener object, which we then push to listeners array.

/**
 * Add event listener for a single key using a keycode.
 * @paramkeyCode Key code of the key.
 * @paramhandlerDown Function to be called when the key is pressed down.
 * @paramhandlerUp Function to be called when the key is released.
 * @paramalt Used in combination with the alt key.
 * @paramctrl Used in combination with the ctrl key.
 * @paramshift Used in combination with the shift key.
 */

public function addListener(keyCode:int, handlerDown:Function = null, handlerUp:Function = null, alt:Boolean = false, ctrl:Boolean = false, shift:Boolean = false):void {
listeners.push(new KeyListener(keyCode, handlerDown, handlerUp, alt, ctrl, shift));
}

Now, in kDown and kUp functions, we simply loop through all listeners elements, compare their keyCodes to the actually pressed keycodes, check if the array element is a KeyListener class instance, and check if altKey, shiftKey and ctrlKey values match the ones from the event object. If all conditions are met, call the handlerD function in kDown and handlerU function in kUp:

private function kDown(evt:KeyboardEvent):void {
for (var i:int = 0; i < listeners.length; i++) {
if (evt.keyCode == listeners[i].keyCode && listeners[i] is KeyListener && evt.altKey == listeners[i].alt && evt.ctrlKey == listeners[i].ctrl && evt.shiftKey == listeners[i].shift) {
if (listeners[i].handlerD) listeners[i].handlerD.call();
}
}
}

private function kUp(evt:KeyboardEvent):void {
for (var i:int = 0; i < listeners.length; i++) {
if (evt.keyCode == listeners[i].keyCode && listeners[i] is KeyListener && evt.altKey == listeners[i].alt && evt.ctrlKey == listeners[i].ctrl && evt.shiftKey == listeners[i].shift) {
if (listeners[i].handlerU) listeners[i].handlerU.call();
}
}
}

Full EasyKeyboard class so far:

package com.kircode.EasyKeyboard 
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;

/**
 * Utility for easy keyboard listener management.
 * @author Kirill Poletaev
 */
public class EasyKeyboard 
{
public var listeners:Array = [];
private var st:Stage;

public function EasyKeyboard(stage:Stage) 
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
stage.addEventListener(KeyboardEvent.KEY_UP, kUp);
stage.addEventListener(Event.ENTER_FRAME, frame);
st = stage;
}

private function frame(evt:Event):void {

}

/**
 * Add event listener for a single key using a keycode.
 * @paramkeyCode Key code of the key.
 * @paramhandlerDown Function to be called when the key is pressed down.
 * @paramhandlerUp Function to be called when the key is released.
 * @paramalt Used in combination with the alt key.
 * @paramctrl Used in combination with the ctrl key.
 * @paramshift Used in combination with the shift key.
 */

public function addListener(keyCode:int, handlerDown:Function = null, handlerUp:Function = null, alt:Boolean = false, ctrl:Boolean = false, shift:Boolean = false):void {
listeners.push(new KeyListener(keyCode, handlerDown, handlerUp, alt, ctrl, shift));
}

private function kDown(evt:KeyboardEvent):void {
for (var i:int = 0; i < listeners.length; i++) {
if (evt.keyCode == listeners[i].keyCode && listeners[i] is KeyListener && evt.altKey == listeners[i].alt && evt.ctrlKey == listeners[i].ctrl && evt.shiftKey == listeners[i].shift) {
if (listeners[i].handlerD) listeners[i].handlerD.call();
}
}
}

private function kUp(evt:KeyboardEvent):void {
for (var i:int = 0; i < listeners.length; i++) {
if (evt.keyCode == listeners[i].keyCode && listeners[i] is KeyListener && evt.altKey == listeners[i].alt && evt.ctrlKey == listeners[i].ctrl && evt.shiftKey == listeners[i].shift) {
if (listeners[i].handlerU) listeners[i].handlerU.call();
}
}
}

}

}

Now we can go to main.as and perform a test. Let's add a listener to the A key (keycode 65). When the key is down - trace "Down", when it's up - trace "Up".

keyboard = new EasyKeyboard(stage);
keyboard.addListener(65, function() { trace("Down"); },  function() { trace("Up"); })

That's all for today.

Thanks for reading!

No comments:

Post a Comment