Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center / AIR Quick Starts for ActionScript developers /

Capturing soft key input

by Sunil Bhaskaran

Sunil Bhaskaran

Content

  • Handling soft key events
  • The Home soft key
  • Where to go from here

Modified

10 October 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Adobe AIR Flash Builder Flash Professional mobile

Requirements

Prerequisite knowledge

Familiarity with ActionScript 3.

 

Additional Requirements

  • A mobile device supporting Android 2.2 or higher.
  • Android SDK

User level

Intermediate

Required products

  • Flash Builder (Download trial)
  • Flash Professional CS5 (Download trial)

Sample files

  • SoftKeysTest.zip
  • SoftKeysTest-android-apk.zip

In Adobe AIR applications, an event is dispatched when a specific activity happens. For example, Adobe AIR generates a keyboard event whenever you press or release a key on the keyboard. Each key is identified by a unique key code. Adobe AIR extends this support to the soft keys used on mobile devices, such as Back, Menu, and Search.

In this article, you will learn about handling soft key events.

Handling soft key events

When you press any of the soft keys, the NativeApplication object dispatches the keyboard  event, KeyboardEvent.KEY_DOWN. Similarly, when you release the key, the NativeApplication object dispatches the event, KeyboardEvent.KEY_UP. You can handle these events and provide the appropriate behavior for these keys when you develop an application.

For example, consider a mobile application in which you have a collection of records. When you press the Back key, you want to display the previous record in the collection. By default, when you press the Back key on an Android device, an Adobe AIR application moves to the background and the phone reactivates the previously active application. Handling the keyboard events that are generated from pressing the Back key allows you to prevent this default behavior and program the application to behave in the way you want.

Note: You must handle the keyDown event when you want to cancel the default behavior of the BACK key. The keyUp event is too late in the event chain.

You can listen for keyboard events on the NativeApplication object. Use the following statement to add a listener for the keyDown event: 

NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN,checkKeypress);

In the following example, the checkKeypress function illustrates how you can test the dispatched KeyboardEvent object to determine which key was pressed. The function uses a switch statement to provide specific behavior for the soft key that is pressed. Other key presses are ignored.

public function CheckKeypress(event:KeyboardEvent):void { switch (event.keyCode) { case Keyboard.BACK: event.preventDefault(); trace("Back key is pressed."); break; case Keyboard.MENU: trace("Menu key is pressed."); break; case Keyboard.SEARCH: trace("Search key is pressed."); break; } }

When designing your application, it is very important to handle the soft keys in a way that is consistent with the native applications on the platform. Otherwise, users may become frustrated when your application doesn't respond in the way they expect. For example, on the Android platform, the Back key is used to return to a previous view of the application until you reach the initial view. From the initial view, another press of the Back key deactivates the current application and reactivates the previous application or the phone Home screen. In an Adobe AIR application, you must keep track of the application state in order to emulate this native behavior. If you do nothing, the Back key will deactivate your application, no matter what state or view it is in.

The Home soft key

A keyboard event is not dispatched when the Home key is pressed. Your application is deactivated and this cannot be prevented. However, the NativeApplication object does dispatch a deactivate event. In the deactivate event handler, you should take the following actions to save battery power and to prepare for possible termination:

  • Save application state
  • Remove event listeners from the Accelerometer and Geolocation objects
  • Stop audio playback
  • Throttle the application frame rate

Note: AIR automatically throttles the frame rate of background applications to four frames-per-second. You can save even more battery power by setting the frame rate to .01, if your application does not have any persistent network connections.

To restore an application, you can use either the activate event or the invoke event. An activate event is dispatched when your application is brought back to the foreground, but not when your app is launched initially. An invoke event is dispatched when your application is brought to the foreground and also when it is launched initially.

Where to go from here

In this article, you learned about handling soft key events. For more information, refer to Basics of handlingevents in the online documentation. 

More Like This

  • Capturing video input in a mobile AIR application
  • Using the StageWebView class
  • Using the tel: and sms: URIs in a mobile phone application
  • Using the Adobe AIR Geolocation APIs on Android

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement