Responding to events and gestures

The most common events are generated by a user. When a user touches the screen or tilts the BlackBerry PlayBook tablet, BlackBerry Tablet OS sends your application a notification of the event. Other events are generated by the BlackBerry Tablet OS. For example, if the battery power level is low, the BlackBerry Tablet OS tries to notify all applications.

The BlackBerry Tablet OS passes event information to your application by using the Adobe Flash event model. In the Flash event model, the BlackBerry Tablet OS tries to notify an object about an event. If the event is not processed, the BlackBerry Tablet OS notifies the next object in the object hierarchy until the event is processed. You can add an event handler to any object by invoking addEventListener(). For example, when you create a button you can add an event handler:

myButton.addEventListener(event, method)

When the event specified by the event parameter occurs, the BlackBerry Tablet OS invokes the method specified by the method parameter

The following table describes some of the API packages related to events.

API package

Description

qnx.events

This package contains event classes that you can use to capture BlackBerry Tablet OS specific events (for example, low battery power level, and requests to release the media service connection).

qnx.fuse.ui.events

This package contains event classes that you can use to capture events related to BlackBerry Tablet OS UI components.

flash.events

This package contains event classes that you can use to capture events that are generated by the Adobe AIR runtime, events related to application state (foreground, background), geolocation, and the accelerometer. You can use this package to detect the following gestures:

  • Pan
  • Rotate
  • Swipe
  • Zoom
  • Hold and press
  • Two finger tap

Respond to an event

The following steps demonstrate how to respond to a swipe gesture.

  1. Import the required classes and interfaces.
    import flash.display.Sprite;
    import flash.events.TransformGestureEvent;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;
    
    import qnx.fuse.ui.text.Label;
    
  2. Add the following statement to your class before the class declaration. This statement sets the width, height, frame rate, and background color of the Stage.
    [SWF(width="1024", height="600", backgroundColor="#cccccc", frameRate="30")]
  3. Create an application framework by extending the Sprite class.
    public class SwipeDemo extends Sprite
    {
        public function SwipeDemo()
        {
        }
    }
  4. Create an instance variable that refers to a Label object. This label reports the direction that the user swipes. Add this statement to your application framework, before your constructor.
    private var _lblStatus:Label = new Label();
  5. Create the initializeUI() method to set up and initialize the UI components.
    private function initializeUI():void
    {
    }
  6. In initializeUI(), configure the label object you created in step 5.Center the label on the screen and add it to the Stage.
    lblStatus.y = (stage.stageHeight - lblStatus.height)/2;
    lblStatus.x = (stage.stageWidth - lblStatus.width)/2;
    lblStatus.width = stage.stageWidth;
    lblStatus.text = "Drag your finger across the screen";
    
    addChild(lblStatus);
  7. In initializeUI(), set the input mode to GESTURE.
    Multitouch.inputMode = MultitouchInputMode.GESTURE;
  8. In initializeUI(), add an event listener for swipe gestures. Invoke the onSwipe() method when the BlackBerry Tablet OS SDK reports a swipe gesture.
    stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE, onSwipe);
  9. In the constructor for your class, invoke initializeUI().
    initializeUI();
  10. Create the onSwipe() method to process the event.
    public function onSwipe(event:TransformGestureEvent):void
    {
    }
  11. In onSwipe(), create a String variable to hold the interpretation of the swipe event.
    var strSwipe:String = "";
  12. In onSwipe(), test the event's offsetX property to determine if the user swiped left.
    if ( event.offsetX == -1 ) strSwipe = "Swipe direction: Left";
  13. Create tests to detect swipes in other directions.
    if ( event.offsetX == 1 ) strSwipe = "Swipe direction: Right";
    if ( event.offsetY == -1 ) strSwipe = "Swipe direction: Up";
    if ( event.offsetY == 1 ) strSwipe = "Swipe direction: Down";
  14. Set the text property of the Label on the Stage to display the interpretation of the swipe event.
    lblStatus.text = strSwipe;