Consume key events in 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 have posted a bug report on the forum…