<< 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.

Run the program

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.

Resources

I will publish a list containing links to ActionScript resources as a separate document. Search for ActionScript Resources in theConnexions search box.

Complete program listings

Complete listings of the programs discussed in this lesson are provided below.

Code for the file named main.mxml.

<?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>
Listing

Source code for the class named driver.

/*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

Miscellaneous

This section contains a variety of miscellaneous materials.

Housekeeping material
  • Module name: Handling Keyboard Events
  • Files:
    • ActionScript0115\ActionScript0115.htm
    • ActionScript0115\Connexions\ActionScriptXhtml0115.htm
PDF disclaimer: Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

-end-

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask