Forwarding key event such as back button event to unity activity is not working from 4.5

If I forward key event such as back button press to unity activity using dispatchKeyEvent it is not working from unity 4.5.x version on wards. Same method is working fine in unity 4.3.x.

The way I have implemented is show below. I want to keep the same implementation as I am sending some other events as well.

private const int KEY_CODE_BACK = 4;
private const int KEY_ACTION_DOWN = 0;

void Update () {

if (Input.GetKeyDown(KeyCode.Escape)) {
SendKeyEvent(KEY_ACTION_DOWN, KEY_CODE_BACK);
}
if(Input.GetKeyUp(KeyCode.Escape)) {
SendKeyEvent(KEY_ACTION_UP, KEY_CODE_BACK);
}

}

void SendKeyEvent(int action, int code) {
AndroidJavaClass cUnityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
AndroidJavaObject oCurrentActivity = cUnityPlayer.GetStatic(“currentActivity”);
oCurrentActivity.Call(“runOnUiThread”, new AndroidJavaRunnable(() => {
AndroidJavaObject oKeyEventDown = new AndroidJavaObject(“android.view.KeyEvent”, action, code);
oCurrentActivity.Call(“dispatchKeyEvent”, oKeyEventDown)
}
}

I have also tried sending the event to the Window of the activity. In that case calling sequence is Activity.getWindow().getCallback().dispatchKeyEvent().

But if I call onBackPressed directly using the activity object then it is working fine, but I don’t want to use in this way.

Complete source code. Back button are handled properly in 4.3 but not in 4.5

using UnityEngine;
using System.Collections;

public class UIElement : MonoBehaviour {

private const int KEY_CODE_BACK = 4;
private const int KEY_ACTION_DOWN = 0;
private const int KEY_ACTION_UP = 1;
private const int KEY_CODE_ENTER = 66;

private string mStatusMessage = “”;

void Start () {
}

void Update () {
if(Input.GetKeyDown(KeyCode.Escape)) {
SendKeyEvent(KEY_ACTION_DOWN, KEY_CODE_BACK);
mStatusMessage = “Escape button pressed”;
Debug.Log(“KeyEventTest :” + mStatusMessage);
}
if(Input.GetKeyUp(KeyCode.Escape)) {
SendKeyEvent(KEY_ACTION_UP, KEY_CODE_BACK);
mStatusMessage = “Escape button released”;
Debug.Log(“KeyEventTest :” + mStatusMessage);
}

if(Input.GetKeyDown(KeyCode.Return)){
SendKeyEvent(KEY_ACTION_DOWN, KEY_CODE_ENTER);
mStatusMessage = “Enter button pressed”;
Debug.Log(“KeyEventTest :” + mStatusMessage);
}
if(Input.GetKeyUp(KeyCode.Return)){
SendKeyEvent(KEY_ACTION_UP, KEY_CODE_ENTER);
mStatusMessage = “Enter button released”;
Debug.Log(“KeyEventTest :” + mStatusMessage);
}
}

void OnGUI () {
int w = Screen.width / 2;
int h = Screen.height / 2;
int x = Screen.width / 4;
int y = Screen.height / 4;

int bw = w/2;
int bh = Mathf.Min(h/4, 64);
int margin = 10;

int bx = x+(-bw+w)/2;
int by = y+(h-(2bh+2margin))/2;

GUIStyle cs = new GUIStyle(GUI.skin.box);
cs.fontSize = 64;

GUIStyle bs = new GUIStyle(GUI.skin.button);
bs.fontSize = 32;

GUI.Box(new Rect(x, y, w, h), “Press A Button”, cs);

if (GUI.Button(new Rect(bx+margin, by+margin + bh, bw, bh), “Back Button”, bs)) {
SendKeyEvent(KEY_ACTION_DOWN, KEY_CODE_BACK);
SendKeyEvent(KEY_ACTION_UP, KEY_CODE_BACK);
}

if (GUI.Button(new Rect(bx+margin, by+2margin+2bh, bw, bh), “Enter Button”, bs)) {
SendKeyEvent(KEY_ACTION_DOWN, KEY_CODE_ENTER);
SendKeyEvent(KEY_ACTION_UP, KEY_CODE_ENTER);
}

GUIStyle ls = new GUIStyle(GUI.skin.label);
ls.fontSize = 24;
GUI.Label(new Rect(bx+margin, by+4margin+4bh, w, bh), mStatusMessage, ls);
}

void SendKeyEvent(int action, int code) {
AndroidJavaClass cUnityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
AndroidJavaObject oCurrentActivity = cUnityPlayer.GetStatic(“currentActivity”);
oCurrentActivity.Call(“runOnUiThread”, new AndroidJavaRunnable(() => {
// Obtain window callback
AndroidJavaObject oWindow = oCurrentActivity.Call(“getWindow”);
AndroidJavaObject oCallback = oWindow.Call(“getCallback”);

// Send key events
AndroidJavaObject oKeyEventDown = new AndroidJavaObject(“android.view.KeyEvent”, action, code);
oCallback.Call(“dispatchKeyEvent”, oKeyEventDown);
}));
}

}

Attaching test application.

1861417–119427–KeyEventTest.zip (195 KB)

For an Android photo app, I am trying to get keyUp event for VOLUME_UP key with JNI without native plugin… I’ve tried tvranjith code on 2017.2.03 but didn’t work… Is it possible to get Volume Key events?