How to Use hardware(H/W) MENU KEY

How to use the basic hardware keys on the phone.(home key, menu key, back key, find key)

To run the application after the other event, I want to know

1 Like

Back key is KeyCode.Escape, menu key is KeyCode.Menu. Not sure about the others.

Thank you.
Is working properly.

Input.GetKey() can use
Event.keyCode can`t use

run@Emulator

I have the below in my update() loop to detect hardware buttons and exit my app, but I can’t seem to figure out the Home button on the Nexus One. Menu and Escape work great, but KeyCode.Home doesn’t work. When I press Home, I end up on my Home Screen in Android, but my app is still running.

        //if running on Android, check for Menu/Home and exit
        if (Application.platform == RuntimePlatform.Android)
        {
            if (Input.GetKey(KeyCode.Home) || Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.Menu))
            {
                Application.Quit();
                return;
            }
        }

Anyone know what the Home button KeyCode is or a better way to close the app when Home is pushed?

3 Likes

After doing some digging in Android forums, it appears intercepting the Home button is not possible. So how do we close our app when someone hits home? I assume override one of the following:

OnDisable
OnApplicationFocus
OnApplicationPause

Can anyone tell me which is best practice? Thanks!

1 Like

Would appreciate some help on this? or should I just not close our app when someone presses home?

You shouldn’t close your app, when the home button gets hit, you should just pause the game, so the player can get back to the game by holding the home button. Android has real multitasking and I use it all the time, I hate it when a game closes when I accidentally press the home button (which happens all too often, when I play with the Trackball). I also often multitask with the home button to get to my music while playing a muted game or to read a new e-mail.

I see ok. I just read some complaints from people on Android market about apps not closing and they didn’t realize they were running. I agree that multi-tasking is great, but wonder how well the average user understands this.

The problem with apps not closing happens, when there is no dedicated exit button or the game doesn’t close by pressing the return key in the main menu.

The best way to close the app fast is when the return key gets pressed two times, where you give a rumble after the first press.

Hi.

Can someone post which button name is used for pressing down the trackball? Also it sure be nice to have an Android User Input section somewhere. Android Prefabs yada yada, what asset packages should you use yada yada…

Thanks.

I think that stuff ends on joystick 1 and its handling.

Yeah I thought that but it wouldin’t work. Just got it by trial and error.

if (Input.GetKeyDown(KeyCode.JoystickButton0)) …

I realize this is an old post but I thought I would mention a lot of games have a pop up that says “would you like to quit” when you hit the home button.

I am just diving into Droid but, the first thing I did before even making a build was download a bunch of the more popular games in the hopes of finding a few established standards.

:slight_smile:

I’m not getting ANY keys, as in Input.anykey, from the system buttons on any of the 3 devices I have. Did this change with 3.3? Anyone have this working can post a whole Update function?

function Update ()
{
//Input.GetKeyDown"escape" is for PC, Input.GetKey(KeyCode.Menu is for Android
if (Input.GetKeyDown(“escape”) || Input.GetKey(KeyCode.Menu))
{
//do whatever you want.
}
}

I tried that. I get nothing. I put in a Debug.Log as well as trying to show my menu, but nothing at all. It also does not respond to Input.anyKey.

EDIT: (smacking own head): Another script was disabling my menu script. I stopped that, now it works ok.

I don’t think android was designed to have applications intercept home key.

Checking key as kalos said works on pressing button. If you press and don’t release, it keeps being pressed → function that needs to be runned just once, runned many times . How to implement doing some action on releasing button?

The scripting reference describes Input.GetKey/GetKeyDown/GetKeyUp. I use Input.GetKeyDown. I expect Input.GetKeyUp should work, also.