Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

« Return

ActionScript 3

Keyboard Event Listener

In a flash movie, capturing key strokes from the user's keyboard can be done using the addEventListener method with an event type of KeyboardEvent. You can then use either the Key Code or the Char Code in your Flash movie. Char Code more accurately shows what letters the user typed, but Key Code should be used for knowing what arrow keys the user pressed.

Download the FLA file

   For this Flash movie to work correctly, the movie must be clicked on before
	 keyboard presses will be registered properly.  This is why the Start button 
	 appears in the upper left.  Because it's enough simply to click on the button
	 to give the Flash movie focus, the button needs no code behind it.

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownFunction);

function myKeyDownFunction(event:KeyboardEvent)
	textBox1.text = event.keyCode.toString();
	textBox2.text = String.fromCharCode(event.keyCode); 
	textBox3.text = event.charCode.toString();	
	textBox4.text = String.fromCharCode(event.charCode); 			

« Return

©2012 Kroll Design    781.910.3694
Last modified: 12/31/1969 7:00 PM