PDA

View Full Version : Reading Key Values


antun
04-09-2004, 01:29 PM
One way of integrating keyboard input into your application is to have a focusable view capture pressed key codes. Only one view can be focusable at any one time. In this example, we're making that view focused oninit (although in a larger app, you could bring focus to that view using the tab key, or you could write an onclick method to tell it to focus):


<canvas debug="true">

<debug x="110" y="15" />

<!-- NOTE: Even though the view below takes focus oninit,
in some browsers you might still need to click on the
application itself, to take the focus away from the
browser itself and to the Flash Player. -->

<view width="100" height="100" bgcolor="0x333399"
focusable="true"
oninit="LzFocus.setFocus(this)">

<method event="oninit">
this.keyCodes = new Array()
this.keyCodes[65] = "A for Apple";
this.keyCodes[76] = "L for Laszlo";
this.keyCodes[79] = "O for Optometrist";
this.keyCodes[83] = "S for Sammy";
this.keyCodes[90] = "Z for Zebra";
</method>

<method event="onkeydown" args="akeycode">
// respond here
Debug.write( "Key pressed: " + akeycode );
if ( this.keyCodes[akeycode] != undefined ) {
Debug.write( this.keyCodes[akeycode] );
} else {
Debug.write( "you pressed: " + String.fromCharCode(akeycode) );
}
</method>

</view>

</canvas>


Enjoy!