Thursday, April 26, 2007

Flash Lite Tutorial: How to Detect Button Presses

In order to create a flash lite menu, one of the most important things to establish is how the flash application will react to a button being pressed on your keypad. In Flash Lite 1.1, the easiest method of achieving this is to use actionscript in conjunction with a button.

Firstly, you will need to create a symbol as a button. After placing it on stage, right-click on it to select Actions and enter this:

on(keyPress ""){
//put in response here
trace("Pressed Enter");
}

"" reflects the key designation you are waiting for. The following lines between the brackets reflect how you want the application to respond to the button being pressed. In this case, the trace() method will cause the text "Pressed Enter" to be displayed in a debug window. For a flash menu system, the action performed by a key press would most likely trigger movement to the next icon in the menu array, but we will look into that later.

*Note: "" is the key designation for the button in the middle of the directional pad. Similarly, the key designation for the up button is "", down button is "". The number pad designation follows as "1","2", etc..



Cellphone Button Keypress Designation

0-9, *, #

Select key

Left Arrow key

Right Arrow key

Up Arrow key

Down Arrow key

Left soft key

Right soft key

0, 1, 2, 3, 4, 5, 6, 7, 8, -9, *, #




compile to see what happens. In our example, you can see what happens:





It's important to keep in mind that this method does not require you to physically place the button somewhere. In the subsequent flash menu tutorials, you will see that the button is placed off of the stage so it can not be seen. Thus, for a flash menu, it acts more of a listener than anything.

In Flash Lite 2.0, you can actually create movie clip listeners that will listen for any key being pressed and return the identity of the key being pressed. For more indepth differences,

Flash Lite Tutorial: The Soft Keys (Flash Lite 1.1)

The Soft Keys (Flash Lite 1.1)


These buttons are especially useful in navigation and would seem logical to include these in menu selection. Traditionally, the left soft key in a menu selects and activates that menu, while the right soft key exits the menu system. However with more advanced cellphones with keyboard functions, there can be more than 2 soft keys. We will take a look into how this is done.


First off, we need to look at two pieces of code:

FSCommand2("setSoftKeys", soft1, soft2);

This is an fscommand2() function, which contrasting fscommand() is immediately gets activated without waiting for the frame to end. This command needs to be at the beginning of your flash file for your soft keys to be operable. The variables soft1 and soft2 represents the labels you will have on your keys (shown at the bottom left and right side of your screen). Thus, for your menu, if you want the left button labelled Select and right button labelled Exit, you would do this:

FSCommand2("setSoftKeys", "Select","Previous");

Now you have another decision to consider, do you want your flash file to be fullscreen? There are two ways around this:

a) Yes! I want fullscreen!
You will need this piece of code:

FSCommand2("fullscreen", "true");

Here's the added bonus: you will see the labelled buttons appear at the bottom (via your first line). That is to say, you'd see Select and Previous labels.

b) No, no fullscreen!
This is acceptable as well, the flash still shows fullscreen (*I'm referring to implementing menus here, not just swf files - if you're just viewing a swf file, it will not be fullscreen in this instance). However, only a left label that says "Select" will be shown. Although your soft keys will be functional, you will not see the assigned labels you used. In this method, you will also need to allocate some room at the bottom movie to accomodate for the label. Or else, that "Select" label will overlap anything you have there.

And lastly, now that we have that sorted out, you're ready to use your soft keys!

on(keyPress ""){
//do your action here
}

Remember in Flash 1.1, left soft key refers to , and right soft key refers to .