13KeyboardEvent class with two key event properties:
KEY_DOWNKEY_UPkey events will only fire for objects that have focus.
focus property of the Stage class.
the default for single items like individual text boxes in a form is to receive a glowing border with focus.
this can be turned off with the stageFocusRect property of the Stage class.
keys of the keyboard are assigned unique numbers by the flash player so they can be identified in code. these numbers come in two flavors: ascii codes, or virtual codes.
the American Standard Code for Information Interchange is a mapping of the numbers 0—127 to letters of the english alphabet, punctuation, numbers, and various control codes. ⊚.
when a key is typed on the keyboard, a KeyboardEvent is generated that stores the ascii code for the key.
we can retrieve the ascii code through the charCode property of the KeyboardEvent.
import flash.events.KeyboardEvent; stage.addEventListener( KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void { trace(e.charCode); } );
but the ascii standard is very old, designed before keyboards had arrow keys, function keys, number pads, control, alt, shift, page up, page down, caps lock, etc.
so the KeyboardEvent also stores virtual key codes for typed keys to ensure consistency between operating systems.
we can retrieve the virtual key code through the keyCode property of the KeyboardEvent.
import flash.events.KeyboardEvent; stage.addEventListener( KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void { trace(e.keyCode); } );
Keyboard class
has several properties that store common virtual key code numbers in a more intuitive label:
Keyboard.BACKSPACEKeyboard.CAPSLOCKKeyboard.CONTROLKeyboard.DELETEKeyboard.DOWNKeyboard.ENDKeyboard.ENTERKeyboard.ESCAPEKeyboard.HOMEKeyboard.INSERTKeyboard.LEFTKeyboard.PGDNKeyboard.PGUPKeyboard.RIGHTKeyboard.SHIFTKeyboard.SPACEKeyboard.TABKeyboard.UPswf]
[fla]
[as]
swf]
[as]
[fla]
TextField class enable us to display text to the user on the stage, and accept text input from the user.
text fields come in three flavors:
text and htmltext propertiesTextField instance.
static text fields can only be created from within flash.
import flash.text.TextField; var tf:TextField = new TextField(); addChild(tf); tf.text = "i am a textfield";
TextField instance directly:
multilineselectabletextColorwordWrapTextFormat class allows for more robust formatting control.
embedFont property of a TextField instance to true.swf]
[.fla]
var s1:String = "qbert's my hero!"; var s2:String = "$#!@"; var s3:String = "1 + 1 = more than 1.";but some characters would confuse the compiler, like if you wanted to put quote marks in a string. for such characters you can escape them by preceding them with a backslash (
\):
var s4:String = "so she was like, \"yah\", and i was like, \"totally\", you know?";trace the above to see: so she was like, "yah", and i was like, "totally", you know?
\t // tab (ASCII 9) \n // line feed (ASCII 10) \r // carriage return (ASCII 13) \" // double quote \' // single quote \\ // backslash \u0000 // a 16-bit unicode character specified in hex [0000, ffff]
+. it is the same operator we use to perform
addition on numerical values. you use it to 'add' one string to the end of another:
var pre:String = "snow"; var post:String = "shoe"; var together:String = pre +post; // "snowshoe"this is very handy for sending detailed information to the ouput window:
var n:Number = Math.floor( Math.random() * 100); trace("of all the numbers i could have chosen, i picked " +n);multiple values can be concatenated together in one line:
trace(_name +":\n " +_width +" x " +_height +" pixels");note that no matter what values are used during a string concatenation, the result is always a string:
var s5:String = "area" + 51;consider the following:
function pad(original:Number, padding:String, places:Number):String { var padded:String = String(original); while (padded.length < places) { padded = padding + padded; } return padded; }
String object provides many handy methods for string manipulation:
charAt()
var i:String = ("equinox").charAt(3);
charCodeAt()
var oneHundredFive:String = ("equinox").charCodeAt(3);
fromCharCode()
var i:String = String.fromCharCode(105); var secretMessage:String = String.fromCharCode( 66,101,32,83,117,114,101,32,84,111,32,68,114,105,110,107, 32,89,111,117,114,32,79,118,97,108,116,105,110,101,46 );
indexOf()
var three:Number = ("equinox").indexOf("i"); var one:Number = ("vegetable").indexOf("e");
lastIndexOf()
var eight:Number = ("vegetable").lastIndexOf("e");
split()
var alphabetArray:Array = ("abcdefghijklmnopqrstuvwxyz").split(); var p:String = alphabetArray[15];
substring()
var innerSiesta:String = ("interesting").substring(4, 8);
toLowerCase(), toUpperCase()
var shouting:String = ("hey").toUpperCase(); var match:Boolean = userInput.text.toLowerCase() == "yes";