Subscribe

  • Subscribe  

Using KeyCodes in ActionScript 3

Posted by Jay | May 7, 2008 .

When I started getting a hang of new stuff introduced in AS3, I got to notice that it has done away with a bunch of commands that were available in AS2. One of these missing commands included “Key.isDown” and added too that there were simply no key objects in AS3. Instead it has an alternative process whereby key codes can be accessed by name basically utilizing the keyboard class.

Now lets take a closer look; in AS3 in “Key.isDown” is basically an event handling process which is made out of a two step process

  • Write an event handler (listener function in AS3), event handler is a function which executes in response to the event e.g. key press.
  • In AS3 event has also a class representation per se “KeyboardEvent”. We use the addEventListener() method to map listener function with the target of the event e.g. key press which is an object of “KeyboardEvent” class.

To elaborate the above concept of keyboard event handling Actionscript3 has build in “KeyboardEvent” and “keyboard” class. keypress event can be elaborated as

  • Mapping the key press event with the listener function e.g.
   addEventListener(KeyboardEvent.KEY_UP,keyHandlerfunction) 

Or

   addEventListener(KeyboardEvent.KEY_down, keyHandlerfunction)

Here KEY_UP and KEY_DOWN are constants for respective key up or key down events. Key down event takes place when a key is pressed and reverse Key up event takes place when a pressed key is released.

keyHandlerfunction function will be executed in response to key up or key down event.

package 
{
   import flash.display.DisplayObject;
   import flash.events.*;

   public class Event_Example extends DisplayObject {
     private var child: DisplayObject = new DisplayObject ();

    // register the listener function
    public function Event_Handle_Example() 
    {
       addChild(child);
       child.addEventListener(KeyboardEvent.KEY_UP, keyHandlerfunction);
    }

   // listener funtion
   private function keyHandlerfunction (event:KeyboardEvent):void 
   {
      trace("keyHandlerfunction: " + event.keyCode);
    }
}

Leave a Comment

If you would like to make a comment, please fill out the form below.


Name

Email

Website

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comments

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Related Posts

9 Comments so far
  1. shankar thapa  July 1, 2008 1:59 am

    nice tutorial.. :)

  2. Ravinder  July 5, 2008 3:58 am

    Hey this is very good and simple example to understand Key handling in AS3. It is really helpfull.

  3. Jim D  November 4, 2008 8:48 pm

    Not a good tutorial. It fails to show how to catch specific words or keys.

  4. Sebastian Andersson  January 5, 2009 5:40 am

    I’ve spent some 200 hours developing a nice telnet & vt100 terminal emulator for flash player 9. Works pretty well on my development machine (OS X), but then I tried it on XP and Vista. At first it seemed ok, but then I noticed that there were keys that I could not write… On a swedish keyboard layout, shift+2 is supposed to give ‘”‘, but it gives ‘@’ as CharCode in flash. Shift-7 is supposed to give ‘/’, but it gives ‘&’ and there are a few other errors. So to be able to use KeyBoardEvent.charCode, one has to translate it per keyboard layout and operating system, but not even that will work since some key-combinations like those needed to write a ~ character on a swedish keyboard does not give any events at all (the next character after ~ will give an extra event, if the ~ character is supposed to be written on its own).

  5. Andrew  April 22, 2010 11:35 am

    If you are writing a tutorial, please make sure the code work, what do we link it to? is there anything on the stage atm, or if not why is nothing being called dynamically. Please don’t post a tutorial if it is useless like this one seems to be. I tried it, and the code clearly did not work

  6. Kristin  August 12, 2010 12:12 pm

    The political ad on your web site, “Paid for by California Working Families for Jerry Brown for Governor 2010″

    Working Families is a front group for ACORN.

    If you support their strong-arm tactics, and Obama’s destruction of America, I pity you.

    If you don’t, I beg you, please remove the ad from your site.

  7. Andrew  September 25, 2010 2:02 pm

    Hello. I have a small problem with the e.keyCode. I’m trying to pause my game but the function only triggers at “Shift + p” …. not at “p”. How can I solve this?

  8. Eelco  January 13, 2011 6:26 am

    Very nice, but on my system where the keys autorepeat, with a pause before the second down-up, the keyup comes before anything is done, so in the end, nothing happens.

  9. fuckyou  May 19, 2011 10:57 am

    hey man, thanks so much. I was looking to make a program which gave me nothing but errors and this was just what I was looking for

    fuck you you piece of shit. no one in the world seems to be able to give me a simple tutorial on button presses.

<

Error. Page cannot be displayed. Please contact your service provider for more details. (2)


Direct TV Offers - usdirect has the best directtv deals