Best Practice for Key Detection in Flash Lite 2

Using Button
In Flash Lite 1.1, if we want to detect a key press (single press or continuous press), we need to attach the code to a button:
[code lang=”actionscript”]on (keyPress ““) {
no += 1;
}[/code]
In Flash Lite 2, we can use back modern ActionScript, use Movie Clip or Object to do the key detection, for example:

Using Movie Clip as Key Listener[code lang=”actionscript”]mc.onKeyDown = function() {
switch (Key.getCode()) {
case Key.UP :
no += 1;
break;
}
};
Key.addListener(mc);[/code]
Using Object as Key Listener[code lang=”actionscript”]obj = new Object();
obj.onKeyDown = function() {
switch (Key.getCode()) {
case Key.UP :
no += 1;
break;
}
};
Key.addListener(obj);[/code]
However, using key listener has a side effect, you cannot detect continuous key press (press the key without release). If you want to detect continuous key press, you need to use an MovieClip.onEnterFrame:[code lang=”actionscript”]mc.onEnterFrame = function() {
if (Key.isDown(Key.UP)) {
no += 1;
}
};
//Key.addListener(obj);[/code]
By the way, I find the Flash Lite 2 emulator is even worse than Flash Lite 1.1 emulator because the latter one support pressing keyboard for testing (single press or continuous press), no need to use the mouse to click the phone button.

Here is the summary of the above testing:

Continuous Pressing Detecting Method Press without Release in Handset Press phone button without Release Press the key in the PC keyboard without Release
FL 1.1 Emulator FL 2 Emulator FL 1.1 Emulator FL 2 Emulator
Button Support (Slower) Not Support Not Support Support Not Support
Object Not Support N/A Not Support N/A Not Support
Movie Clip Not Support N/A Not Support N/A Not Support
Movie Clip onEnterFrame Support (Faster) N/A Support N/A Not Support

Download the source files

5 Replies to “Best Practice for Key Detection in Flash Lite 2”

Comments are closed.