Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by aristigon2 · Apr 01, 2016 at 04:05 AM · androidbuttonhardwareescape

How can I detect if the back button is virtual or physical? Hardware or Software Device buttons

I want to display a UI exit or back button if the android device has virtual menu buttons instead of physical ones. Many phones have physical hardware buttons, whereas most tablets have virtual buttons. I have had complaints with my games cause people don't know to swipe up to get the menu so they can back out.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fredex8 · Apr 01, 2016 at 06:37 AM 0
Share

Good question because it's something I hadn't actually thought about before and I hate it when apps don't take advantage of my phone having hardware buttons.

A quick search would suggest the keycodes are Escape and $$anonymous$$enu but one site mentions Home so try that too I guess.

avatar image aristigon2 · Apr 01, 2016 at 02:23 PM 0
Share

I can make the hardware buttons work just fine. That isn't the issue. I want to display a button on the screen when there is not a hardware button, so the player does not have to swipe up first just to press the back button.

avatar image Fredex8 aristigon2 · Apr 01, 2016 at 05:34 PM 0
Share

Ah sorry I must have skim read the question. I did a bit of searching around as I'd be interested to know how to do this too.

Found a few similar questions but no solution. I thought maybe it would be possible to use the keycode to check if that button actually exists. I mean laptops don't have dedicated numpads and different keyboards do have extra or missing keys sometimes so it seems logical. Couldn't find any way of doing that though.

The only solutions I can think of would be to check what the device is on first run and compare it to a list of devices which says which buttons exist for each device and which don't. Would be far from ideal of course and it would take ages to write the list given the number of devices out there. Another solution would just be to have an options menu so the player can manually choose to use hardware buttons rather than on screen, again not ideal as some people will try and turn it off even if they have no buttons. Another option could be to have the user calibrate the device on first run by instructing them to press the menu and back buttons on their device and if the input is true turn off on screen buttons for that button. At least that way the user wouldn't be able to turn off on screen buttons if they don't have the hardware ones. I'm sure there must be an automatic and more intuitive way to do this though.

avatar image Bonfire-Boy Fredex8 · Sep 28, 2016 at 12:39 AM 0
Share

Isn't that last idea (the calibration one) a bit circular? How would you confirm that the button the user had pressed was a physical button?

avatar image meat5000 ♦ · Apr 01, 2016 at 08:50 PM 1
Share

I think you might need to access the Android API for this one. Ive not needed to do that myself, so far but perhaps you can use this:

http://developer.android.com/reference/android/view/ViewConfiguration.html#hasPermanent$$anonymous$$enu$$anonymous$$ey%28%29

in conjunction with this:

http://docs.unity3d.com/ScriptReference/AndroidJavaObject.Call.html

Information on creating plugins for Android and accessing them with AndroidJavaObject can be read here:

http://docs.unity3d.com/$$anonymous$$anual/PluginsForAndroid.html

On another note, looking at your comment:

Immersive mode is only included on later versions of Android and there are a number of devices that permanently display software buttons, so for this it is not an issue. For the rest, it seems that disabling Immersive mode is what can be done to simply display the buttons. Perhaps it can be included as a setting in the Options, of whether or not the user wants the bar displayed or not.

https://www.google.co.uk/search?q=unity+disable+immersive+mode&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=yd_-VqWVD-jW8geUj5HACA&gws_rd=ssl

avatar image aristigon2 meat5000 ♦ · Apr 02, 2016 at 02:57 PM 0
Share

That is what I was thinking. I think I had found a way to do it but it was with the Android API. I was having trouble accessing it in Unity though.

avatar image meat5000 ♦ aristigon2 · Apr 02, 2016 at 04:18 PM 0
Share

$$anonymous$$y Android API knowledge is quite limited and as such the syntax and structure baffles me a little.

I found a post on SO which seems to have a good method without requiring the ND$$anonymous$$. It uses the Android Java plugin interface so you can use the AndroidJavaObject class to interact with it.

http://stackoverflow.com/questions/9121781/calling-android-ndk-function-from-unity-script

Just installing Android Studio so I can learn this.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
8

Answer by meat5000 · Apr 04, 2016 at 12:15 PM

Solution for Android Only:

Ok I succeeded I do believe. Took 2 days of melting my brain trying to work through all the problems i encountered along the way.

This class interacts with the Plugin and calls its methods.

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class AccessButtonsType : MonoBehaviour{
 #if UNITY_ANDROID
   private AndroidJavaObject testobj = null;
     private AndroidJavaObject playerActivityContext = null;
  
     void Start() {
         if (testobj == null) {
             // First, obtain the current activity context
             using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                 playerActivityContext = actClass.GetStatic<AndroidJavaObject>("currentActivity");
             }
  
             // Pass the context to a newly instantiated UnityGetButtonsType object
             using (var pluginClass = new AndroidJavaClass("com.tmg.getbuttons.UnityGetButtonsType")) {
                 if (pluginClass != null) {
                     testobj = pluginClass.CallStatic<AndroidJavaObject>("instance");
                     testobj.Call("setContext", playerActivityContext);
                 }
             }
         }
     }
   
   public static string GetButtonsT() {
     string emptyString = null;
     if( Application.platform != RuntimePlatform.Android )
       return emptyString;
 
     var pluginClass = new AndroidJavaClass("com.tmg.getbuttons.UnityGetButtonsType") ;
     AndroidJavaObject plugin = pluginClass.CallStatic<AndroidJavaObject>("instance");
     return plugin.Call<string>("GetButtonsType");
   } 
   #endif
 }

I threw this in to test and control the .cs class.

 using UnityEngine;
 using System.Collections;
 
 public class AccessScript : MonoBehaviour {
     
     public string isIt;
     void Start ()
     {
         isIt = "Empty";
     }
     
     void Update ()
     {
         isIt = AccessButtonsType.GetButtonsT();
         if(isIt == null)
             isIt = "Empty";
         Debug.Log(isIt);
     }
 
     void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 100, 20), isIt + " Buttons");
     }
 }

This is the .java plugin I compiled in Android studio and dropped into the Plugins/Android folder in .jar format. I didnt need to target JDK 1.6. I didn't need any XML manifest in the plugins folder.

 package com.tmg.getbuttons;
 
 import android.content.ContentValues;
 import android.content.Intent;
 import android.content.Context;
 import android.os.Environment;
 import android.view.ViewConfiguration;
 
 public class UnityGetButtonsType {
 
     Context context;
     private static UnityGetButtonsType m_instance;
 
     public static UnityGetButtonsType instance() {
         if(m_instance == null)
             m_instance = new UnityGetButtonsType();
         return m_instance;
     }
 
     public UnityGetButtonsType(){
         m_instance = this;
     }
 
     public void setContext(Context ctx) {
         this.context = ctx;
     }
 
     public String GetButtonsType() {
         ViewConfiguration vConfig = ViewConfiguration.get(this.context);
 
         if (vConfig.hasPermanentMenuKey()) {
             return "hard";
         } else {
             return "soft";
         }
     }
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Apr 04, 2016 at 12:16 PM 0
Share

You may need to change package names etc to match your project.

It does throw an error in the editor, which is supposed to be remedied by the #if Android tags, but this doesnt work as its in Android mode in the editor too. You should add in 'if not editor'.

On Device you will see "Soft Buttons" or "Hard Buttons" in the top left.

$$anonymous$$odify the code to fit your needs.

avatar image meat5000 ♦ · Apr 04, 2016 at 12:48 PM 0
Share

Oh yes, here's the resources I drew the information from.

http://stackoverflow.com/questions/9121781/calling-android-ndk-function-from-unity-script

http://www.thegamecontriver.com/2015/04/android-plugin-unity-android-studio.html

http://www.vortech.net/2013/03/accessing-the-android-activity-context-in-unity3d/

avatar image aristigon2 · Apr 04, 2016 at 01:47 PM 0
Share

Wow, thanks. I had been busy working on another project and you took this and solved it. I will use this. Thank you very much.

avatar image Dave-Carlile aristigon2 · Apr 19, 2016 at 12:04 PM 2
Share

Quite a bit of work went into this answer. In return how about taking some time to mark it as accepted?

avatar image JonyUps · Sep 08, 2016 at 01:04 PM 0
Share

Can you share this .jar file?

avatar image meat5000 ♦ JonyUps · Sep 27, 2016 at 04:32 PM 0
Share

You compile the .java file into a .jar file yourself using the links provided for direction.

avatar image MASTware · Mar 07, 2017 at 11:02 PM 0
Share

Great work! Thanks. You've got a thump up from me. But I have a problem with the hasPermanent$$anonymous$$enu$$anonymous$$ey method. On Samsung Galaxy S7 the method returns false (-> "soft"). But the Galaxy S7 has a permanent menu key. What's wrong?

avatar image meat5000 ♦ MASTware · Mar 28, 2017 at 11:22 AM 0
Share

No idea. I'll look into it if I get a chance. I've been out of the loop for a few months.

avatar image meat5000 ♦ MASTware · Mar 10, 2018 at 03:15 PM 0
Share

I suspect the keys are software configurable so perhaps they make them contextually soft to make life easier for themselves.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a button 3 Answers

Check that the joystick is being pressed for a few seconds 1 Answer

A few android questions. 0 Answers

Remote Gui Button only works on device if mouse is over the button in Unity editor 0 Answers

Android back button not registering 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges