# Using buttons

You can let users trigger actions by placing a FormButton or NavigationButton control in a view. Use FormButtons for actions that start some process and NavigationButton controls for situations where pressing the button should take the viewer to another view.

Figure 1. FormButton controls

You can find out when a button has been pressed by registering a callback function as an event listener to the button. The event type for button presses is "ActionPerformed". You can use whatever name you like for the callback function. The function is passed the event object as an argument. A typical callback function looks like this:

    // Callback function for button presses.
    function buttonPressed(event) {
        // implement what happens when the button is pressed here
    }

Creating a button, registering the event listener and adding the button to a view is demonstrated below. The example creates a FormButton:

    // create button
    var button = new FormButton("button1", "Press me!");
    button.addEventListener("ActionPerformed", buttonPressed);
    exampleView.addControl(button);

Note that the example assumes that a view has already been created and that the "exampleView" variable refers to that view. "button1" is a unique identifier for the button that allows it to be identified for example if several different controls are using a shared event listener function. The event listener function could examine the event object and check the identifier of the source control for the event.

Parent topic: Common WRTKit tasks