<< Chapter < Page | Chapter >> Page > |
The charCode property
Each time the method is called, it receives a reference to an object of type KeyboardEvent . This object encapsulates several types of information about the key that was pressed. One such piece of information is a propertynamed charCode . Here is some of what the documentation has to say about this property:
"Contains the character code value of the key pressed or released. The character code values are English keyboard values. For example, ifyou press Shift+3, charCode is # on a Japanese keyboard, just as it is on an English keyboard."
The keyCode property
Another interesting property of the incoming object is the property named keyCode . This property can be used to identity any key on the keyboard, including thosethat are not represented by printable characters such as the shift key and the arrow keys.
Modify the text in targetLabel
The objective of the code in Listing 5 is to modify the text in the label referred to by targetLabel to cause it to reflect the character for the key that was pressed.
Convert charCode to a string
The text property of the label is type String . That means that the value of charCode must be converted to type String .
This is accomplished by calling the static fromCharCode method of the String class.
Because this is a static method, it can be called by joining its name to the name of theclass to which it belongs: String .
Assign the returned String value to the text property
The method returns a reference to a String object containing the character whose charCode is passed as a parameter to the method. This string is then assigned to the text property of the target label, producing the output shown in Figure 2.
I encourage you to run this program from the web. Then copy the code from Listing 6and Listing 7. Use that code to create your own project. Compile and run the project. Experiment withthe code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.
I will publish a list containing links to ActionScript resources as a separate document. Search for ActionScript Resources in theConnexions search box.
Complete listings of the programs discussed in this lesson are provided below.
<?xml version="1.0" encoding="utf-8"?><!--
KeyboardEvent02See explanation in the file named Driver.as
--><mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:Driver/></mx:Application>
/*Prob04 06/03/10
Illustrates the use of KeyboardEvent, charCode values,absolute positioning on a Canvas object, focus, and a
TextArea object among other things.See http://livedocs.adobe.com/flex/3/langref/flash/events
/KeyboardEvent.html*********************************************************/
package CustomClasses{import flash.events.KeyboardEvent;
import flash.events.MouseEvent;import mx.containers.Canvas;
import mx.controls.Label;import mx.controls.TextArea;
public class Driver extends Canvas{//Instantiate and save references to the
// objects needed by the program.private var instrArea:TextArea = new TextArea();
private var targetLabel:Label = new Label();private var canvasLabel:Label = new Label();
//--------------------------------------------------//public function Driver() {//constructor//Set the size of the Canvas object.
this.width = 300;this.height = 120;//Prepare the TextArea and the labels.
canvasLabel.text = "This is a 300x120 Canvas";instrArea.text = "First click the yellow canvas "
+ "with the mouse\nThen press a key to display "+ "the character.";
instrArea.width = 298;instrArea.height = 40;
instrArea.x = 1;instrArea.y = 26;targetLabel.setStyle("fontSize", 30);
targetLabel.x = 10;targetLabel.y = 78
//Display an empty string at startup.targetLabel.text = "";//Add the labels and TextArea to the Canvas.
this.addChild(canvasLabel);this.addChild(instrArea);
this.addChild(targetLabel);//Set the Canvas background color to yellow.this.setStyle("backgroundColor", "0xFFFF00");//Register two event listeners on the canvas.
this.addEventListener(MouseEvent.CLICK, clickHandler);
this.addEventListener(KeyboardEvent.KEY_DOWN,eventHandler);
} //end constructor//--------------------------------------------------//
//This method is executed when any key is pressed.// Note, however, that some keys, such as the shift
// key, don't have displayable charCode values.private function eventHandler(
event:KeyboardEvent):void {targetLabel.text =
String.fromCharCode(event.charCode);} //end eventHandler
//--------------------------------------------------////This event handler is required to cause the Canvas// to gain the focus and respond to keyboard events.
// See the URL listed earlier. (Note that focus can// also be gained by pressing the tab key.)
private function clickHandler(event:MouseEvent):void {stage.focus = this;
}//end clickHandler//--------------------------------------------------//
} //end class} //end package
This section contains a variety of miscellaneous materials.
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?