Handling Mouse in Android


The intention of this section is not to teach about using the mouse in Android. It is to show changes NVIDIA has made to its build of Android that may be of interest to developers. The foundations on input you have learned by implementing controller support in your application should make mouse support trivial.

Cursor Visibility

A new function, setCursorVisibility(boolean) in InputManager (API16), can turn the drawing of the mouse pointer on or off. Remember to reset visibility when your application is resumed. A developer needs to use reflection to make sure this method is available. Google has an excellent blog post on reflection here:

http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html

Mouse RelativeX and RelativeY

Two new fields, AXIS_RELATIVE_X and AXIS_RELATIVE_Y in MotionEvent, will give you the relative (x, y) position from the previous position. These fields can allow full “mouselook” in first-person applications. A developer needs to use reflection to make sure these fields are available. See Cursor Availability above for a link on using reflection in Android.

Sample Code

Below is an example of using reflection for the new features.

//*****************************************************************************
class Wrap_NVMouseExtensions { private static Method mInputManager_setCursorVisibility;
private static int nMotionEvent_AXIS_RELATIVE_X = 0;
private static int nMotionEvent_AXIS_RELATIVE_Y = 0;
//**************************************************************************
static {
try { mInputManager_setCursorVisibility =
InputManager.class.getMethod("setCursorVisibility", boolean.class);
}
catch (NoSuchMethodException ex) { throw /* some exception */; }

try { Field fieldMotionEvent_AXIS_RELATIVE_X =
MotionEvent.class.getField("AXIS_RELATIVE_X");
try { nMotionEvent_AXIS_RELATIVE_X = (Integer)
fieldMotionEvent_AXIS_RELATIVE_X.get(null);
}
catch (IllegalAccessException ex) { throw /* some exception */; }
}
catch (NoSuchFieldException ex) { throw /* some exception */; }
/* DO THE SAME FOR RELATIVEY */
}
//**************************************************************************
public static void checkAvailable () { /* force initialization above */ };
//**************************************************************************
public static boolean setCursorVisibility(InputManager im, boolean fVisibility) {
try { mInputManager_setCursorVisibility.invoke(im, fVisibility); }
catch (InvocationTargetException ite) { return false; }
catch (IllegalAccessException iae) { return false; }
return true;
}

//**************************************************************************
public static int getAxisRelativeX() { return nMotionEvent_AXIS_RELATIVE_X); };
public static int getAxisRelativeY() { return nMotionEvent_AXIS_RELATIVE_Y); };
}
//*****************************************************************************
public class SimpleMouseTests extends Activity {
int gnAPILevel;
InputManager inputManager;
boolean gfPtrVisibility;

//***** For reflection checking of NVIDIA mouse extensions
private static boolean gf_NVMouseExtensions;

static {
try { Wrap_NVMouseExtensions.checkAvailable();
gf_NVMouseExtensions = true;
}
catch (Throwable t) { gf_NVMouseExtensions = false; }
}
//**************************************************************************
@Override
protected void onCreate(Bundle savedInstanceState) {
gnAPILevel = Build.VERSION.SDK_INT;
gfPtrVisibility = true;
inputManager = null;
if (gnAPILevel >= Build.VERSION_CODES.JELLY_BEAN) {
inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
}
}

//**************************************************************************
@Override
public void onResume() {
setMouseVisibility(gfPtrVisibility);
super.onResume();
}
//**************************************************************************
public void setMouseVisibility(boolean fVisibility) {
if (! gf_NVMouseExtensions) return;
gfPtrVisibility = fVisibility;
Wrap_NVMouseExtensions.setCursorVisibility(inputManager, gfPtrVisibility);
}
//**************************************************************************
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
int nGetAction = event.getActionMasked();

if ((nSourceBits & InputDevice.SOURCE_MOUSE) == 0) {
return super.dispatchGenericMotionEvent(event);
}
if (gf_NVMouseExtensions) {
float flRelativeX =
event.getAxisValue(Wrap_NVMouseExtensions.getAxisRelativeX(), 0);
float flRelativeY =
event.getAxisValue(Wrap_NVMouseExtensions.getAxisRelativeY(), 0);
}
return true;
}
}

 

 


NVIDIA® GameWorks™ Documentation Rev. 1.0.200601 ©2014-2020. NVIDIA Corporation. All Rights Reserved.