13
text

key events

flash provides the KeyboardEvent class with two key event properties:

focus

key events will only fire for objects that have focus.

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.

key codes

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.

ascii key 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); }
);

virtual key codes

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); }
);

label constants

remembering arbitrary numbers is usually harder than remembering a common-sense name, so the Keyboard class has several properties that store common virtual key code numbers in a more intuitive label:

examples

key code utility

it's often convenient to see all the codes and a nice label together in one spot. click the image below to give it focus; then type keys on the keyboard to see their code numbers and a description.

Alternative content

[swf] [fla] [as]

movable character

arrow keys are common for providing the user control over onscreen characters:
[swf] [as] [fla]

 

text fields

the 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:
  1. static—display only; if not set as selectable, text turns into shapes when compiled
  2. dynamic—programmer can read & write through the text and htmltext properties
  3. input—a dynamic text field that user can input characters into by selecting and typing
you can create text fields with the text tool in flash, or with code that instantiates a new TextField 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";

font formatting

very basic formatting options can be set on a TextField instance directly: the TextFormat class allows for more robust formatting control.

font embedding

for dynamic or input text fields to display in the correct font, and to work with graphical transformations like rotation or alpha-blending, the font outline information needs to be embedded in the swf. this can be accomplished in one of two ways: [.swf] [.fla]

strings

we've been using strings regularly to send message to ourselves through the output window, but they deserve more attention. in order to communicate with the user, we'll want to be able to compose and accept complex messages.

special characters

spaces, numbers, punctuation, math operators, and the other characters at the top of the keyboard are all ok to include in a literal string value:
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?
other commonly escaped characters:
\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]

concatenation

strings can be connected to form longer strings with the concatenation operator: +. 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

the String object provides many handy methods for string manipulation:

 

further investigations