Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Use hardware(H/W) MENU KEY

Discussion in 'Android' started by darkmages, Sep 29, 2010.

  1. darkmages

    darkmages

    Joined:
    Jun 25, 2010
    Posts:
    7
    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
     
    Last edited: Sep 29, 2010
    senseiGoodie likes this.
  2. beezir

    beezir

    Joined:
    Apr 3, 2009
    Posts:
    133
    Back key is KeyCode.Escape, menu key is KeyCode.Menu. Not sure about the others.
     
  3. darkmages

    darkmages

    Joined:
    Jun 25, 2010
    Posts:
    7
    Thank you.
    Is working properly.
     
  4. Teuton

    Teuton

    Joined:
    Jun 24, 2010
    Posts:
    15
    Input.GetKey() can use
    Event.keyCode can`t use

    run@Emulator
     
    Last edited: Oct 5, 2010
  5. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    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.

    Code (csharp):
    1.         //if running on Android, check for Menu/Home and exit
    2.         if (Application.platform == RuntimePlatform.Android)
    3.         {
    4.             if (Input.GetKey(KeyCode.Home) || Input.GetKey(KeyCode.Escape) || Input.GetKey(KeyCode.Menu))
    5.             {
    6.                 Application.Quit();
    7.                 return;
    8.             }
    9.         }
    Anyone know what the Home button KeyCode is or a better way to close the app when Home is pushed?
     
  6. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    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!
     
    senseiGoodie likes this.
  7. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    Would appreciate some help on this? or should I just not close our app when someone presses home?
     
  8. Dan Fury

    Dan Fury

    Joined:
    Jul 18, 2010
    Posts:
    158
    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.
     
    Last edited: Oct 7, 2010
  9. akasurreal

    akasurreal

    Joined:
    Jul 17, 2009
    Posts:
    442
    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.
     
  10. Dan Fury

    Dan Fury

    Joined:
    Jul 18, 2010
    Posts:
    158
    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.
     
  11. Tomo-Games

    Tomo-Games

    Joined:
    Sep 20, 2010
    Posts:
    223
    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.
     
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I think that stuff ends on joystick 1 and its handling.
     
  13. Tomo-Games

    Tomo-Games

    Joined:
    Sep 20, 2010
    Posts:
    223
    Yeah I thought that but it wouldin't work. Just got it by trial and error.

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

     
  14. JamesMobot

    JamesMobot

    Joined:
    Jul 8, 2010
    Posts:
    170
    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.

    :)
     
  15. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    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?
     
  16. kalos

    kalos

    Joined:
    Dec 11, 2010
    Posts:
    121
    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.
    }
    }
     
  17. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    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.
     
    Last edited: Jun 6, 2011
  18. NestorAlgieri

    NestorAlgieri

    Joined:
    Sep 11, 2011
    Posts:
    299
    I don't think android was designed to have applications intercept home key.
     
  19. Rustam-Ganeyev

    Rustam-Ganeyev

    Joined:
    Jul 18, 2011
    Posts:
    29
    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?
     
  20. Deleted User

    Deleted User

    Guest

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

    Rustam-Ganeyev

    Joined:
    Jul 18, 2011
    Posts:
    29
    yeah, it's working as it should. Thanks!
     
  22. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    How do you use the HW back and search key?
     
  23. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Google is officially supporting the actionbar menu key, and deprecating the hardware menu key. It would be nice if Input.GetMenu gracefully scales to that for Unity Android 3.x+ builds

    http://www.pcmag.com/article2/0,2817,2399437,00.asp and other articles
     
  24. safia1230

    safia1230

    Joined:
    Mar 2, 2012
    Posts:
    1
    I'm currently trying to add a click listener to the menu hardware button. Currently I'm just putting my onclick logic into the onCreatePanelMenu-method and return false. But that just feels wrong.

    Is there a more clean way?

    The code currently looks like that:

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
    Toast.makeText(this, "HALLO!", Toast.LENGTH_SHORT).show();
    return false;
    }
     
  25. Choong How

    Choong How

    Joined:
    Nov 28, 2011
    Posts:
    7
    Does anyone happen to test out the back button on HTC Sensation? I've tried Input.GetKey, Input.GetKeyUp and Input.GetKeyDown with the escape key but none seem to work. Is there a problem with Unity reading keypress from Sensation or are there some settings which needs to be done before building the apk which I'm unaware of? Thanks.

    Problem fixed.
    Placed code in FixedUpdate and problem is solved.
     
    Last edited: Mar 19, 2012
  26. waldroid

    waldroid

    Joined:
    Apr 11, 2012
    Posts:
    7
    i want to create android project with eclipse.. in the main menu of the project, there is a button to call the AR ..
    when pressing the back button can be moved to the main menu? if can, can you please help me?

    thank u :)
     
  27. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    Does anyone know how to use the trackball?

    I would expect it to come in one of the joysticks, but we get no messages at all from the trackball.
     
  28. keithsoulasa

    keithsoulasa

    Joined:
    Feb 15, 2012
    Posts:
    2,126
    I'd suggest against this, since most new phones dont even have a trackball .
     
  29. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    I don't want to rely on it, just like I wouldn't rely on xperia play touchpad. However for the phones that do have it, I would like to support it as an additional input method.
     
  30. BusterTheWizard

    BusterTheWizard

    Joined:
    Jul 2, 2012
    Posts:
    55
    Just as a side note for anyone that needs it, I found the keycodes to the hardware camera focus/shutter button(s).
    Focus(if the device has it): KeyCode.F6
    Shutter: KeyCode.F5

    I've only tested this on my Evo 4G LTE, so I don't know if it works for any other phones.
     
  31. sajidanouman

    sajidanouman

    Joined:
    Mar 24, 2013
    Posts:
    1
    Nice Thanks Alot For Sharing
     
  32. trs9556

    trs9556

    Joined:
    Apr 15, 2013
    Posts:
    9
    I have been trying to figure out how to detect when the home button is pressed for a while and although it isn't "specifically for the home button" it works.


    void OnApplicationFocus(bool pauseStatus) {
    if(pauseStatus) { //code to close app or w\e you want to do }
    }

    When you press the home button, the application loses focus so OnApplicationFocus is called and passes true through it's parameters.

    I have no idea what else might make OnApplicationFocus equal true, probably any time to user puts your app in the background, aka opening a text/email. So only use if you can.

    I wanted to know the home button thing because I wanted to save data. So using this method works just as good if not better.
     
  33. davididev

    davididev

    Joined:
    Jan 21, 2014
    Posts:
    1
    I find on older phones if I define an axis for escape and menu, I can still use those buttons.
    Instead of Input.GetKey(KeyCode.Menu)
    I use Input.GetAxis("MenuAxis");
     
  34. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    Any solution exist for detecting device Menu or Home button press?
    I can't use OnApplicationPause method for quit application because my game contains In App Purchase functionality and it was calling multiple times OnApplicationPause method.
     
  35. cdr9042

    cdr9042

    Joined:
    Apr 22, 2018
    Posts:
    170
    Have you found the solution yet? I can't use OnApplicationPause either. I need to detect Home button press