user input with keyCode charCode and visual output example

going from actionscript 2 a2 to as3 can be tough, and this forum will show the road to as3. as3 is not best suited for small animation or web projects, and is in someways a red headed stepchild of java and c++. But, it still has a purpose, for now.
Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

user input with keyCode charCode and visual output example

Post by darknkreepy3# »

using the charCode and keyCode functions can be extremely confusing at times, so I created this demo for a student to run through a simple visual example of the process.
It is important to not you cannot run this program while you are in the flash IDE by default, as when you press keys in the IDE, the IDE itself senses the keys first, uless you turn shortcuts off (bad idea for interactive GUI programming in the flash IDE, its a time saver)

so, tear this down and check it out

http://www.supercala.net/tuts/as3/keycodes/keycodes.zip

Image
Image

keycodes.as

Code: Select all

package
    {
    import flash.display.MovieClip;
    import flash.display.DisplayObject;
    import flash.events.*;
    //-----
    public class keycodes extends MovieClip
        {
        private var keysArr:Array;
        private var keyID:int;
        private var mcKeyArr:Array;
        private var keyAmt:int;
        //
        public function keycodes():void
            {
            trace("keycodes()");
            //
            keysArr=new Array();
            /*
            in strings a single \ backslash and then character deonotes a special character
            so to denote a backslash itself, you need to escape one with itself \\
            */
            keysArr[0]="1234567890-=\\";
            keysArr[1]="qwertyuiop[]";
            keysArr[2]="asdfghjkl;'";
            keysArr[3]="zxcvbnm,./";
            //
            mcKeyArr=new Array();
            trace(mc_debug);
            stage.addEventListener(KeyboardEvent.KEY_UP,showKey);
            drawKeys();
            }
        public function showKey(event:KeyboardEvent):void
            {
            var keyAscii:int=0;
            trace("key:"+event.keyCode);
            mc_debug.tF_debug.text="[keyCode]"+event.keyCode+"[charCode]"+event.charCode;
            //
            for(var a:int=0;a<keyAmt;a++)
                {
                //
                if(mcKeyArr[a].ID==event.charCode)
                    {
                    mcKeyArr[a].hilight.visible=true;
                    }
                }
            }
        //
        public function drawKeys():void
            {
            trace("drawKeys()");
            //
            var len:int=keysArr.length;
            var strLen:int;
            var a:int;
            var b:int;
            var tempX:int;
            var tempY:int;
            var charCodeID:int;
            var tempChar:String;
            keyAmt=-1;
            var tgt:mc_key;//simplifies the coding instead of having mcKeyArr[keyAmt] for each reference in the loop
            //
            for(a=0;a<len;a++)
                {
                tempY=64*a;//space lines by 64 vertically
                strLen=keysArr[a].length;//how long is each string of characters?
                trace(keysArr[a]);
                //
                for(b=0;b<strLen;b++)
                    {
                    keyAmt++;            
                    /*
                    space to the next key from the last key's left side                        
                    add the indentation of each line with the loop(a)*24 as well to emulate keyboard staggering of lines
                    */
                    tempX=(24*a)+48*b;
                    tgt=mcKeyArr[keyAmt]=new mc_key();//movieclip in keycodes.fla library                    
                    tempChar=keysArr[a].substr(b,1);
                    charCodeID=tempChar.charCodeAt(0);
                    trace(tempChar+":"+charCodeID);

                    tgt.ID=charCodeID;
                    tgt.tF_A.text=tempChar;//each key takes a one character slice from the string in each current array(a loop)
                    tgt.x=tempX;
                    tgt.y=tempY;
                    tgt.hilight.visible=false;
                    addChild(tgt);
                    }
                }
            }
        }
    }
Post Reply