Unity consume key and motion events in android - android

How do I consume key events in Android. I have exported my game to a "Google Android Project" and I have tried both overriding the dispatchKeyEvent() and onGenericMotionEvent(), like this:
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
#Override public boolean dispatchKeyEvent(KeyEvent event)
{
Log.d(String.valueOf(event.getDeviceId()), "Control");
Toast.makeText(this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
#Override
public boolean onGenericMotionEvent(MotionEvent event)
{
Log.d(String.valueOf(event.getDeviceId()), "Control");
Toast.makeText(this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
return true;
}
I have also tried to attach KeyListener handlers directly to the UnityPlayer object, like this:
mUnityPlayer = new UnityPlayer(this);
mUnityPlayer.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
Log.d(String.valueOf(event.getDeviceId()), "Control");
Toast.makeText(UnityPlayerNativeActivity.this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
return true;
}
}
);
But in both cases, the Java code is not triggering. Any ideas why??

So I'm able to get the values of Jostick AXIS by putting the following under the Unity activity in the manifest. This now triggers onGenericMotionEvent(), but not dispatchKeyEvent().
<meta-data
android:name="unityplayer.ForwardNativeEventsToDalvik"
android:value="true" />
I'm assuming this is a bug in (or a 'feature of') unity, so will file a bug report with them directly

Related

onKeyDown - Control key is not working in Activity

I am trying to add shortcuts in my app when external keyboard is connected. SHIFT+KEY, this combination is triggering the following event. But Control+KEY and Control key events are not triggered. Can anyone suggest me on this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT) {
flag_sellreceipt = KeyEvent.isModifierKey(KeyEvent.KEYCODE_CTRL_LEFT);
int i = KeyEvent.META_CTRL_LEFT_ON;
System.out.println("ctrl is pressed");
}
return
super.onKeyDown(keyCode, event);
}
Note: I have tested this in emulator.

Android: Key Event from Android Box remote controller

I was interested to know how can i catch key/button events from Android TV Box remote controller?
For example, i want a popup menu to show when i click the OK button from remote controller. And i want to catch the next/back key events from remote controller.
Should i use the Key Event class from Android, if yes how should i implement it?
I came across this function but i cannot really make sense of it.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
//your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks in advance.
You should catch key event on dispatchKeyEvent
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Log.e(TAG, "Key down, code " + event.getKeyCode());
} else if (event.getAction() == KeyEvent.ACTION_UP) {
Log.e(TAG, "Key up, code " + event.getKeyCode());
}
return true;
}
Edit:
First, you should know key map of your remote (it is not the same for all kind of Android TV box), the code above will help you know code of key that you press on the remote. For example, i got key code 3 when i press button BACK on remote.
Then, i want when back key pressed, a Toast message will be show:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// You should make a constant instead of hard code number 3.
if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode == 3) {
Toast.makeText(this, "Hello, you just press BACK", Toast.LENG_LONG).show();
}
return true;
}

Android Google Keyboard crashes when capturing KeyEvent.KEYCODE_ENTER

I'm trying to manage the Next event on the Google Keyboard on a Google Nexus 5. I want my application to check user information when the Next button gets pressed.
The code looks like this:
private TextView.OnEditorActionListener GenerateEditorListeners()
{
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_NULL && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
};
}
On a Samsung Galaxy S4 works perfect, but on a Google Nexus 5 the actionId = 0 and the event = null. So I figure out that the Samsung keyboard works fine with this code, but doesn't happen the same with the Google Keyboard.
Any idea on why it's not wokring for Google's keyboards?
EDIT: I've read in this post that Google keyboard has a bug in some LatinIME keyboards.... I'm using a latin one. If that's the problem, how to solve it?
I could solve it by using the OnKeyListener event:
textUserEmail.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
});
Now it works in all keyboards.

Android - Disable Device Back button

I am working on android application using PhoneGap. I need to handle Device back button functionality by using the below code:
import com.phonegap.DroidGap;
public Class MyClass extends DroidGap {
appView.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
finish();
return true;
}
return onKeyDown(keyCode, event);
}
});
}
By using the above code, Application getting exited because i have used finish(); But i want nothing should be happened on click of Device back button. How can i acheive that? Please help me.
Why do you need to do this at the Java level? You can achieve this with Javascript using Phonegap's Event API
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
}

Confused about Android key event handling. Any good explanations?

I'm a relative beginner with Android. Does anybody have a sane explanation for how to listen for keys and soft keys in an EditText/TextView?
I'd love to see a comprehensive tutorial or set of examples.
As I understand it, I can add a KeyListener to my Activity, e.g. onKeyDown(), onKeyUp() but when I try this I can't trigger the events for normal keys only HOME and BACK for example.
I have seen mention of using a TextWatcher but that isn't the same as handling raw key events.
There seem to be a number of half-solutions here on SO. Hoping you can help clear the mists of confusion...
You have to assign a key listener not to activity but rather to EditText itself.
This is what I have to listen to BACK or MENU key events. Simply add this method, without implementing any Interface. I do this in my BaseActivity, from which every Activity inherits.
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(NAME, "Key pressed");
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Log.d(NAME, "Back pressed");
// IGNORE back key!!
return true;
/* Muestra el MenĂº de Opciones */
case KeyEvent.KEYCODE_MENU:
Intent menu = new Intent(this, Menu.class);
// start activity
startActivity(menu);
return true;
}
return super.onKeyDown(keyCode, event);
}
PS: I highly discourage ignoring the back key.
For example:
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
//your code here
}
return false;
}
});
I recently found another way that be stuck using Activity onKeyDown, or event setting a key listener on view (which is not really working with key events from ADB in my case) with view.setOnKeyListener.
Since android P method addOnUnhandledKeyEventListener has been introduced. It allows you to do whatever you need to do when your view is able to catch unhandled key events.
Here is an example of how I used it :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
when (event.keyCode) {
KeyEvent.KEYCODE_UNKNOWN -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
KeyEvent.KEYCODE_SOFT_RIGHT -> {
TODO("Do whatever you need to do.")
true // Specify you did handle the event
}
// etc...
else -> false // Specify you didn't handle the event
}
}

Categories

Resources