0

I am trying to proven the default functionality of the Android home screen button but the KeyboardEvent.KEY_UP event does not fire when the home key is pressed the the tablet i have. (eee transformer prime) Is there another why to detect and stop this functionality?
Here is the code that i am trying to user

import mx.events.FlexEvent;

        // Add the hardware key event handlers to the stage.
        protected function appCompleteHandler(event:FlexEvent):void {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, handleButtons,false, 1);
            stage.addEventListener(KeyboardEvent.KEY_UP, handleButtons,false, 1);
        }

        // Event handler to handle hardware keyboard keys.
        protected function handleButtons(event:KeyboardEvent):void
        {
            trace("Event fired");
            if (event.keyCode == Keyboard.HOME) {
                event.preventDefault();
                trace("home");
            } else if (event.keyCode == Keyboard.BACK) {
                // Hanlde back button.
                event.preventDefault();
                trace("back");
            }

        }
Justin
  • 1,249
  • 1
  • 15
  • 37
  • Does it not even output **"Event fired"**? – inhan Feb 28 '12 at 21:44
  • Nope, It doesn't even show that anything has bin hit. It just exits the app. No trace("Event fired"); at all. – Justin Feb 28 '12 at 22:52
  • So obviously it's not getting either event fired, which tells me that either subject matter button is not considered a key in keyboard, or the application (AIR) looses focus once it's pressed. – inhan Feb 28 '12 at 23:12
  • Have you ever seen this? http://www.adobe.com/devnet/air/quick_start_as/quickstarts/qs_capturing_soft_keys.html I kind of look like you can't prevent it at all. Would it be possible to right a java Extension to over ride the home button using the code provided below? I'm not a java programmer so just wandering if you would know. *edit* Look at the The Home soft key section of the article. – Justin Feb 29 '12 at 00:19

2 Answers2

1

The home button is a special button that you are not allowed to intercept. Your code is correct, however once the KEY_UP event happens your app will be paused and stopped, with no chance to react to KEY_UP.

There is, however, an indirect way. While you cannot stop the HOME key from working, you can make your own HOME activity. The first time you run it you'll be asked which activity you want to use as HOME. From there all you have to do is add a button that, once you enter the password, launches the real home activity.

For more details, look at the answers to this question: Android - Is It possible to disable the click of home button

Community
  • 1
  • 1
num1
  • 4,825
  • 4
  • 31
  • 49
  • So there isn't a way to say require a password be for exiting the app? for example i would like to ask a for a password to be given before the user is able to exit the app. What i am trying to do is make a kiosk style app where the only two ways to exit the app is with a password or shutting down the table and restarting it. – Justin Feb 29 '12 at 00:27
  • @Justin thanks for clarifying what you want to do. I've edited my answer. You can't intercept the home button but you can make a kiosk-like app. – num1 Feb 29 '12 at 01:47
  • This is java right so i would have to build this into a native extension in flex to get it to work right? Thank for your help by the way. – Justin Feb 29 '12 at 17:12
  • I'm sorry I can't help you with that, I don't know anything about adobe flex. Good luck though! – num1 Mar 01 '12 at 20:39
0

Try this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        doSomethingHere();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
ejf
  • 619
  • 6
  • 13
  • Isn't this Java? I'm building the application in ActionScript with adobe air but Thank for the replay. – Justin Feb 28 '12 at 22:54
  • Yep, sorry. It didn't occur to me until after I took another look that this might not be for native android. – ejf Feb 29 '12 at 00:48
  • No worries do you know anything about righting java extension air by chance because that kind of what this question is turning into? The funny thing is since your answer was for Java it gave me a idea about how i might be able to get around the problem I'm running into but I'm still not sure if its possible yet. – Justin Feb 29 '12 at 00:54