Air for Android: Home Menu Back and Search Buttons

Getting information from the Android Device buttons is REALLY nothing you need to stress about. The AIR treats these buttons in the same way that it treats Keyboard events. By listening to the key down event you can get the key code and handle it appropriately for your application.

You can’t override the home button as that is system protected and the other thing to know is that you can prevent the default back, search, and menu actions so that your application can handle it appropriately.

If you are starting with this post I recommend actually starting with a previous post introducing AIR for Android Development. There is a ANT file there that you will want to have.


Here is a quick function that handles the key down event.

        private function _onAddedToStage(event:Event):void
        {
            //removes listener
            removeEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
        }

        private function _onKeyDown(event:KeyboardEvent):void
        {
            if(event.keyCode == Keyboard.BACK)
            {
                event.preventDefault();
                _text.appendText("\nBack Pressed");
            } else if(event.keyCode == Keyboard.MENU)
            {
                event.preventDefault();
                _text.appendText("\nMenu Pressed");
            } else if(event.keyCode == Keyboard.SEARCH)
            {
                event.preventDefault();
                _text.appendText("\nSearch Pressed");
            }
        }
Share

Comments (25)

[...] This post was mentioned on Twitter by FMC Washington DC, Ultra Red and fmchicago, karannnnnnnnnnn3. karannnnnnnnnnn3 said: Air for Android: Home Menu Back and Search Buttons: Getting information from the Android Device buttons is REALLY … http://bit.ly/ad9km2 [...]

Larry LagueSeptember 21st, 2010 at 8:41 pm

This is great! Thanks for the info. However, now that the menu has been activated, is there a way to call the Menu class, and add items to it?

Jonathan CamposSeptember 22nd, 2010 at 1:02 am

@Larry great question. Right now I am skinning a popup to look like the native android app. But hopefully in the future a menu class will be available in flex from adobe.

MikeOctober 12th, 2010 at 11:55 pm

When using Keyboard.BACK I get undefined method or property Keyboard

I imported
import flash.events.KeyboardEvent;

What am I missing?

Jonathan CamposOctober 13th, 2010 at 12:06 am

Are you using Air2.5?

MikeOctober 13th, 2010 at 12:50 am

After 9 hours I realized I had to import

import flash.ui.Keyboard;

Just a note for anyone else who is trying to learn.

MikeOctober 13th, 2010 at 12:51 am

yes, using AIR 2.5

mark munOctober 20th, 2010 at 12:19 pm

hmm…. i’m still seeing an undefined property of BACK in the Keyboard class.

My import flash.ui.Keyboard is there.

using AIR 2.5

Jonathan CamposOctober 20th, 2010 at 12:40 pm

@mark Very odd mark. Make sure you have the newest SDKs and it should be there. If it’s not I really don’t know what the problem would be.

DavidOctober 23rd, 2010 at 10:07 pm

@mark I don’t know if this is any help at all but I found that I was having the same problem described when I had a modified/amended version of the Flex 4.1 SDK running. I found though that when I switched to a version of the Flex 4.0 SDK that I’d modified in the same fashion that the errors all disappeared.

Don’t know if it’s the build of 4.1 that I’ve been using (16076) or what, but I thought that I would pass this on.

alfonsofonsoNovember 20th, 2010 at 8:45 am

Nice, so now we should use NativeApplicationMenu or ContextMenu or WindowMenu??

Jonathan CamposNovember 22nd, 2010 at 8:54 am

@Alfonsofonso do do what?

Mei LingJanuary 28th, 2011 at 1:42 am

I have install adobe Airtime 2.5, however i still getting the undefined method or property Keyboard when go to line Keyboard.BACK, could somebody tell me why?

I have used
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;

jonbcamposJanuary 28th, 2011 at 9:29 am

@Mei Are you using Flex? If so what SDK? I’d recommend using the Flash Builder Burrito build of the SDK so that we know all the necessary classes are up to date.

Mei LingFebruary 7th, 2011 at 10:02 pm

Hi jonbcampos, I’m using Adobe Flash CS4 and Eclipse wit AXDT plugins. Both also got this error message.

How to make sure that the Airtime 2.5 have linked with both of this application before I compile the app to avoid the error message ?

jonbcamposFebruary 7th, 2011 at 10:06 pm

@Mei I believe that you have to be using Flash CS5 to get things going, or eclipse with the Burrito plugin that is currently in labs.adobe.com

MamzelleMarch 6th, 2011 at 4:10 pm

Thanks for the information :) .

101April 10th, 2011 at 9:30 am

@Mei Did you work it out? Are you sure you’re listening for KEY_DOWN and not KEY_UP?

You can also try adding this to your keydown function:

event.stopImmediatePropagation();

yogibaliJuly 25th, 2011 at 10:29 pm

while using Keyboard.MENU it’s work properly, but while using Keyboard.BACK it’s cannot override original android back menu function. any solution? :)

jonbcamposJuly 26th, 2011 at 9:57 am

@yogiball make sure to use event.stopImmediatePropagation(); in your handler to stop the original android back functionality

Matt AlexanderAugust 7th, 2011 at 1:58 am

My soft menu key on honeycomb is not present when i use air sdk 2.7. Anyone else have this issue?

jonbcamposAugust 7th, 2011 at 10:53 am

@Matt I haven’t seen this issue. The soft menu keys are still available on my apps. odd

Jeff KarovaSeptember 20th, 2011 at 2:55 pm

I ran into the same problem as others here with the “undefined method or property ” error on the keyboard class. Basically, constants for BACK, MENU, and SEARCH aren’t defined in my Keyboard class, so I can’t use, say, Keyboard.BACK.

To get around this, in the listener method, I wrote the value of event.keyCode to a textfield, then ran the app and tapped these keys. This gave me the value of the event’s keyCode as a uint, so my evaluation of the event went from this:

if (event.keyCode == Keyboard.BACK)

to this:

if (event.keyCode == 16777238)

It’s not the best solution (the code may change on different hardware), but it worked.

jonbcamposNovember 18th, 2011 at 6:53 am

@qubecity I usually attach to the view.stage for my back handler these days rather than just view. And also do both, event.preventDefault and event.stopImmediatePropagation. works well

yogibaliDecember 14th, 2011 at 12:30 am

thx alot @jonbcampos. yes its work using Keyboard.BACK with event.stopImmediatePropagation() in my handler. but that won’t prevent the action from leaving my screen. all I need is to stay in my apps. :( is there any solution?

Leave a comment

Your comment