Search code examples
androidactionscript-3apache-flexmobileair

Android tablet doesn't fire KeyboardEvent.KEY_DOWN


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

        }

Solution

  • 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