0

As a hobby and to learn, whether this is possible, I'm trying to implement a simple first-person shooter for Android. Unfortunately, I ran into a dead end when dealing with mouse event processing.

I already have an onGenericMotion()-Listener, which processes MotionEvent objects generated by the system framework. The problem is, that MotionEvent objects, generated by a mouse merely contain absolute coordinates, which tend to get "stuck", once the cursor reaches an edge or a corner of the screen. So I'm thinking relative mouse coordinates. While I found no feature on MotionEvent that could deliver relative movements, using

adb shell su -- getevent -lt /dev/input/event3

and examining its output revealed that the kernel generates distinct relative motion events when one tries to move the cursor, even when it is stuck in a corner of the screen. So, given that my shooter has su access, I could obtain relative movements.

And now the question: A little bit of Google-fu revealed, that in many first-person shooters, the typical mouse movement is achieved by

  1. using relative mouse coordinates and
  2. by re-positioning the mouse cursor in the center of the screen.

So, the question really is two-folded:

  1. Is it possible to re-position the mouse cursor in the center of the screen on Android? and
  2. If not, can the typical "first-person-shooter" mouse movement be realised by using relative mouse movement information alone?
Lynn
  • 10,425
  • 43
  • 75
  • Yes :) Using a mini pc [like this one](http://www.amazon.de/MK-Android-Rockchip-RK3188-Cortex/dp/B00BGC9DT0/ref=sr_1_1?ie=UTF8&qid=1394216664&sr=8-1&keywords=android+mini+pc). A little odd, I know ;) – Daniel Eberts Mar 07 '14 at 18:25

2 Answers2

1

This is now possible without root permissions, with the pointer capture API in Android 8.0+ (released August 2017). In summary:

To request pointer capture, call the requestPointerCapture() method on the view.

Once the request to capture the pointer is successful, Android calls onPointerCaptureChange(true), and starts delivering the mouse events.

Your focused view can handle the events by performing one of the following tasks:

  1. If you're using a custom view, override onCapturedPointerEvent(MotionEvent).

  2. Otherwise, register an OnCapturedPointerListener.

The view in your app can release the pointer capture by calling releasePointerCapture().

Lynn
  • 10,425
  • 43
  • 75
-1

It turns out that this completely answers my question. Never mind then.