Set mouse position in software
Asked Answered
C

2

10

I am using an Android Stick (http://www.geekbuying.com/item/Uhost-2-Dual-Core-TV-Box-Mini-PC-Android-4-0-4-RK3066-Cortex-A9-1-6GHZ-1GB-RAM-4G-ROM-with-Bluetooth-WIFI-Skype-XBMC---Black-312467.html) for building an application. The application uses an attached USB webcam for some of it's functionality. Additionally, I connect a mouse to this device which the user can use to navigate through various pages in the application. A left/right movement of the mouse results in navigation to previous/next page.

While the mouse works with the Android device, I additionally require to reset the position of the mouse to the center after every single interaction with the user. Is it possible to set the mouse position using software in Android? I am using View.OnGenericMotionListener to capture the mouse movement.

Currently, I also require to perform a primary mouse button click to bring the mouse in focus inside the application. I want to remove this requirement by either generating the primary mouse button click in software, or otherwise bring the application in software by some other means.

I have been unable to find any APIs to get the above to work. Any help on these would be greatly useful.

Just in case I need to write some sort of drivers to get this thing working, any help in this direction would also be useful.

Any workarounds around this problem, while still using the mouse, could also prove useful.

Cottbus answered 10/12, 2013 at 18:52 Comment(7)
A mouse... in Android?Amann
@RobertHarvey: Well, probably... Though I don't like to have a mouse on my phone/tablet.Detrain
@RobertHarvey - yes, actually, mice have been supported in software for several versions now, and phones with a working USB OTG host mode will activate one if it is plugged in through an OTG adapter cable. kind of funny to watch, but an easy way to test that host mode works.Kartis
@RoberHarvey - Yes. It is possible to connect a mouse to most of the Android phones these days. In my case I am using it with an Android TV Box Mini PC (geekbuying.com/item/…)Cottbus
Let me get this clearly, first, you have already got your mouse cursor working, then you are going to 1. programmatically move the mouse cursor to the center of your screen 2. perform a left click, right?Curtate
@Robin: Here is the sequence.. (1) Do a left click of the mouse inside app in order to bring mouse pointer in focus for the app (need a software solution here) (2) User moves his mouse left or right, and displayed page changes accordingly (this step is fine) (3) Once the user has made one mouse movement, the position of cursor needs to be reset to the centre (need a software solution here). Note that the left click is done only once to bring mouse in focus for the app. Thanks...Cottbus
Check out the following links: - Android Overlay Mouse Cursor - Programmatically Injecting Events on AndroidGoggles
C
3

Mouse event is managed by the system framework. You cannot control it on Java side.

On the adb shell you can open /dev/input/uevent device to write mouse events include

  1. relative movement
  2. click action
  3. absolute position (you might want this)

However, you cannot do it as a normal application, unless you do it on a rooted device, or you can use adb shell to start a daemon service in the background to perform the event writing for your application.

Curtate answered 14/12, 2013 at 6:6 Comment(4)
These sorts of android devices often either come rooted or have holes which could be used for that purpose.Kartis
I can try writing some scripts to access the mouse device in /dev and linking it with the app somehow. But I am looking for better solution. My device is already rooted.Cottbus
Btw, what are the commands for controlling the mouse which should be written to /dev/input/uevent. If you have any online references, please guide me.Cottbus
You can plug in a mouse device and use getevent command to see it.Curtate
B
0

I additionally require to reset the position of the mouse to the center after every single interaction with the user.

This is now possible with the pointer capture API in Android 8.0+ (released August 2017). 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().

Broadbrim answered 17/11, 2020 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.