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.
{
//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");
}
}






[...] 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 [...]
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?
@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.
When using Keyboard.BACK I get undefined method or property Keyboard
I imported
import flash.events.KeyboardEvent;
What am I missing?
Are you using Air2.5?
After 9 hours I realized I had to import
import flash.ui.Keyboard;
Just a note for anyone else who is trying to learn.
yes, using AIR 2.5
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
@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.
@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.
Nice, so now we should use NativeApplicationMenu or ContextMenu or WindowMenu??
@Alfonsofonso do do what?
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;
@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.
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 ?
@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
Thanks for the information
.
@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();
while using Keyboard.MENU it’s work properly, but while using Keyboard.BACK it’s cannot override original android back menu function. any solution?
@yogiball make sure to use event.stopImmediatePropagation(); in your handler to stop the original android back functionality
My soft menu key on honeycomb is not present when i use air sdk 2.7. Anyone else have this issue?
@Matt I haven’t seen this issue. The soft menu keys are still available on my apps. odd
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.
@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
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?