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

Creating an override plugin

Discussion in 'Android' started by GonzoCubFan, Jan 21, 2014.

  1. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    I've read the docs on how to create an override plugin for Android, and I've been semi-successful. I have actually gotten the onCreate call to override properly and display a log message as indicated in the docs. However, I can't seem to get an override of either dispatchKeyEvent() or onKeyDown() to work at all. It seems that the override function never gets called.

    I'm hoping that someone who has had success with this might be able to point out the error of my ways.

    Here is my java code:

    Code (csharp):
    1.  
    2. package com.gonzo.testoverride;
    3.  
    4. import com.unity3d.player.UnityPlayerActivity;;
    5.  
    6. import android.os.Bundle;
    7. import android.util.Log;
    8. import android.view.KeyEvent;
    9.  
    10. public class testOverride extends UnityPlayerActivity
    11. {
    12.     protected void onCreate(Bundle savedInstanceState)
    13.     {
    14.  
    15.         // call UnityPlayerActivity.onCreate()
    16.         super.onCreate(savedInstanceState);
    17.  
    18.         // print debug message to logcat
    19.         Log.d("OverrideActivity", "onCreate has been called!");
    20.     }
    21.  
    22.     @Override
    23.     public boolean dispatchKeyEvent( KeyEvent event )
    24.     {
    25.         Log.d( "OverrideActivity", "dispatchKeyEvent called!" );
    26.         return super.dispatchKeyEvent( event );
    27.     }
    28.  
    29.     @Override
    30.     public boolean onKeyDown( int keyCode, KeyEvent event )
    31.     {
    32.         Log.d( "OverrideActivity", "onKeyDown called!" );
    33.         return super.onKeyDown( keyCode, event );
    34.     }
    35. }
    36.  
    Note that I have tried this both with and without the "@Override" with the exact same results. What you see above is the latest attempt.

    Here is the AndroidManifest.xml that I'm using.

    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gonzo.testoverride">
    4.   <application android:icon="@drawable/app_icon" android:label="@string/app_name">
    5.     <activity android:name="com.unity3d.player.UnityPlayerActivity" android:launchMode="singleTask" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    6.     </activity>
    7.     <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:launchMode="singleTask" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    8.       <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    9.     </activity>
    10.     <activity android:name="com.gonzo.testoverride.testOverride"
    11.               android:label="@string/app_name"
    12.               android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    13.         <intent-filter>
    14.             <action android:name="android.intent.action.MAIN" />
    15.             <category android:name="android.intent.category.LAUNCHER" />
    16.         </intent-filter>
    17.     </activity>
    18.   </application>
    19. </manifest>
    20.  
    Once again, the log message from onCreate shows up just fine, but none of the messages from either onKeyDown() or displatchKeyEvent() ever appear - and yes, I am typing on the keyboard into a GUI.TextField.

    Thanks in advance for any help or insight you can offer. I'm not a Java mavin by any means, and I've only gotten this far by a painful about of hacking. Android is not my favorite platform, primarily because of the incredibly lax standards that they impose on soft keyboard implementations. That, along with Unity's washing their hands of the whole raft of keyboard issues that they choose to ignore rather than address, has soured me considerably on Android right now. Hopefully that will change some day...

    = Ed =
     
  2. Kennethljj

    Kennethljj

    Joined:
    Feb 13, 2013
    Posts:
    17
    Hi,

    I don't see a point in handling Key Events in Java as you are able to handle them in Unity with less hassle.
    If you really want to handle them in java, you may consider working with Unity's JNI instead of overriding the methods.
     
  3. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    Thanks for responding. First of all, I'm trying to grab the keyboard event before Unity/Android OS does silly things with some of the keys that I wish to "disable". JNI gets this data too late after Unity has already dealt with it. Secondly, as documented, JNI can be really slow.

    I appreciate your trying to help, but let me apologize for not being completely clear on my intent. Without that context, your reply makes sense. However, given what I am trying to do here it doesn't.

    Unity's Keyboard handling, especially as it applies to Android, is abysmal at best. Their documentation on the plugin capability is little short of horrid. I am trying to deal with both of these issues :(

    = Ed =
     
    Last edited: Jan 22, 2014
  4. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    Anyone?? Bump!
     
  5. Kennethljj

    Kennethljj

    Joined:
    Feb 13, 2013
    Posts:
    17
    Hi Gonzo,

    No worries. I've seen a few posts similar to what you are looking for but can't remember what the solution was.
    May I know what are you referring to as keyboard events? As in a keyboard appears for players to enter input? Or just an interaction with a GameObject etc?

    My suggestion for now is to look into Unity's source code. (Open source FTW)
    You may want to try and modify the portion which handles the event inputs, interrupt the event and set it to send information or do whatever that you need to.
    However, there is a downside, as this will also cause other events to go haywire if not handled properly as modifying Unity's codes are not advised.

    That's all that I can help you with and what you can try out in the meantime till you find another solution.
     
  6. mushroomrisotto

    mushroomrisotto

    Joined:
    Dec 9, 2013
    Posts:
    24
    try with commenting out //return super.onKeyDown( keyCode, event );
     
  7. GonzoCubFan

    GonzoCubFan

    Joined:
    Oct 21, 2011
    Posts:
    70
    In the case of my test program, that would leave the keyboard inoperable. Not what I'm after. That said, It would certainly point out if the method is being invoked at all. Seems unlikely as that would imply that Log.d() suddenly no longer works in that context.

    = Ed =
     
  8. chetanbansiwal

    chetanbansiwal

    Joined:
    Apr 10, 2015
    Posts:
    1
    Did you get it working?? I need some help regarding same issue...
     
  9. guoang

    guoang

    Joined:
    Apr 29, 2015
    Posts:
    1
    I have the same problem. Did anyone get solution?
     
  10. ackro

    ackro

    Joined:
    Aug 15, 2014
    Posts:
    2
    try to add this line to your testOverride activity:
    <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
    actually I dont see reason to keep the other two activities in the manifest... but its ok they dont eat too much space :)
     
  11. unity_r71wO1KFVJaK9w

    unity_r71wO1KFVJaK9w

    Joined:
    Dec 15, 2020
    Posts:
    7
    I would really appreciate any help regarding this. I want to make a VNC app so I need that the external keyboard every key down and up event will happen and observed inside unity. Currently some keys are not working as expected perform system functionalities on android like opening google assistant on pressing command key on BT keyboard.