Home Contact RSS

Archive for August, 2008

Actionscript Key Listener Tutorial

Overview

Allowing users to use the keyboard as well as the mouse is a great way to incite interaction with your flash. This tutorial will show how to code it and what you can do with some keyboard events. This changed with actionscript 3, note this tutorial is AS3.
altKeY (Boolean) Indicates whether the Alt key is active (true) or inactive (false).
charCode (uint) Contains the character code value of the key pressed or released.
ctrlKey (Boolean) Indicates whether the Control key is active (true) or inactive (false).
keyCode (uint) The key code value of the key pressed or released. KeyboardEvent
keyLocation (uint) Indicates the location of the key on the keyboard. KeyboardEvent
shiftKey (Boolean) Indicates whether the Shift key modifier is active (true) or inactive (false).

Steps

  1. import KeyboardEvent,
    import flash.events.KeyboardEvent;
  2. assign any keycodes
    //keycodes
    var left:uint = 37;
    var up:uint = 38;
    var right:uint = 39;
    var down:uint = 40;
  3. add event listener KeyboardEvent.KEY_DOWN
    stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
  4. respond to keys
    function keyDownListener(e:KeyboardEvent) {
            trace(e.keyCode.toString());
    }

    or

    function keyDownListener(e:KeyboardEvent) {
            if (e.keyCode==left){
    		ship.x-=10;
    		ship.rotation = 90;
    	}
    	if (e.keyCode==up){
    		ship.y-=10;
    		ship.rotation = 180;
    	}
    	if (e.keyCode==right){
    		ship.x+=10;
    		ship.rotation = 270;
    	}
    	if (e.keyCode==down){
    		ship.y+=10;
    		ship.rotation = 0;
    	}
    }

Example

Here we have a swf with the keyboard event listener on the stage, and feedback boxes to give us all we can know about the event. It will tell us about certain keys (alt, ctrl (cmd), and shift) with a Boolean, it will tell us the keyCode and the charCode. The keyCode is the number that is tied to the actual button pressed or key, and the charCode relates to the character represented by the button(s) pressed. So hitting ’s’ and then hitting ’shift + s’ will tell you different charCodes, ’s’ and ‘S’. but you’ll see that the s key has the same keyCode (you’ll also see the ’shift’ keyCode as well). If needed you can use the String.fromCharCode function to determine what the charCode for something is. The location on the keyboard is even reported, this helps distinguish between the left shift and the right shift and even the numbers across the qwerty and the numpad on the right of the screen.
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Actionscript

  1. import flash.events.KeyboardEvent;
  2.  
  3. //keycodes
  4. var left:uint = 37;
  5. var up:uint = 38;
  6. var right:uint = 39;
  7. var down:uint = 40;
  8.  
  9. stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
  10.  
  11. function keyDownListener(e:KeyboardEvent) {
  12.  
  13. feedbackAlt.text = e.altKey.toString();
  14. feedbackCharCode.text = e.charCode.toString();
  15. feedbackChar.text = String.fromCharCode(feedbackCharCode.text);
  16. feedbackCtrl.text = e.ctrlKey.toString();
  17. feedbackKey.text = e.keyCode.toString();
  18. feedbackLoc.text = e.keyLocation.toString();
  19. feedbackShift.text = e.shiftKey.toString();
  20.  
  21. if (e.keyCode == left){
  22. ship.x-=10;
  23. ship.rotation = 90;
  24. }
  25. if (e.keyCode == up){
  26. ship.y-=10;
  27. ship.rotation = 180;
  28. }
  29. if (e.keyCode == right){
  30. ship.x+=10;
  31. ship.rotation = 270;
  32. }
  33. if (e.keyCode == down){
  34. ship.y+=10;
  35. ship.rotation = 0;
  36. }
  37. }

Download

source file download: key-listener.fla

Tags: , , , , ,