Actionscript Course

Keyboard Events (triggered when a key from the keyboard is pressed) are detected with the KeyboardEvent object.
The KeyboardEvent comes in two types:


KeyboardEvent has 6 properties:
- The code values for "charCode" and "keyCode" are dependent on the operating system and keyboard being used. The examples here are for Windows.
- To detect in a Flash presentation which key is pressed, apply the addEventListener() method (with KeyboardEvent) to Stage (to the stage object).

Here's an example with "KeyboardEvent" and KEY_DOWN, in which the "keyCode" property is used.
We create a Flash presentation with a rectangle (or what shape you want) that can be moved with the arrow keys. Also, it uses the "shiftKey" property to modify the motion speed when the Shift key is pressed.
- Follow this steps:
  1. Open a new Flash document, ActionScript 3.0, and draw a rectangle (or what shape you want) on the Stage.
  2. Select the shape on the Stage and convert it into MovieClip (from Modify -> Convert to Symbol, and for Type choose Movie Clip, and press OK).
    Then, open the Properties panel, and add the instance name rectangle for this MovieClip object (in the box with "<Instance Name>").
  3. Right-click on Frame 1 in the Timeline, choose Actions, then in the "Actions panel" add the following code:
    // Register a KeyboardEvent, applied to the "stage" object, to detect when a key is pressed
    // addEventListener is applied to the 'stage' object, to be active in all the presentation
    stage.addEventListener(KeyboardEvent.KEY_DOWN, moveF);
    
    // Function called by the registered event
    function moveF(key_evt:KeyboardEvent):void
    {
      // define a variable with a value that depends on "shiftKey" (used to set the motion speed)
      var vi:int = key_evt.shiftKey ? 12 : 2;
    
      // define a "switch" instruction with values and instructions for arrow keys
      // uses the code valuess for the arrow keys: Left, Right, Down, Up
      switch (key_evt.keyCode)
      {
      case 37:
        rectangle.x -= vi;
        break;
      case 39:
        rectangle.x += vi;
        break;
      case 38:
        rectangle.y -= vi;
        break;
      case 40:
        rectangle.y += vi;
        break;
      default:
        break;
      }
    }
    
  4. Press Ctrl+Enter to test the result. The following Flash presentation will appear:
    - When you press the arrows keys, the rectangle will move in the direction indicated by each arrow (first click on the presentation).
    - If you hold the Shift key pressed, the motion speed will increase.
- The expression "var vi:int = key_evt.shiftKey ? 12 : 2;" defines the value of 12 if "key_evt.shiftKey" is TRUE (when Shift is pressed) or 2 if it's FALSE (Shift is not pressed).
- The "switch" instruction modifies the 'x' and 'y' values for the "rectangle" instance (X and Y distance), with the value of the "vi" variable, depending on the arrow key you press (a "case" for each arrow key).

• To find out the code for each key, you can use the following ActionScript code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyC);
function keyC(key:KeyboardEvent):void { trace(key.keyCode); }
// Displays in the Output panel the code value of the key pressed

To download the FLA file with this example, click: Keyboard events.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
KeyboardEvent - Events for Keyboard

Last accessed pages

  1. The Mastery of Love (7440)
  2. Get Mime Type of file or string content in PHP (6230)
  3. Countdown Timer with starting time added into a form (11533)
  4. Disable button and Enable it after specified time (17533)
  5. JpGraph - Create Graph, Charts, Plots in PHP (3933)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)