I'm trying to develop a Virtual Gamepad
but I'm facing lots of problems sending input and event to Android
, especially with MotionEvents
. I have a device rooted and the app stored in /system/app
, required to send events to the system, but I'm not able to receive them in an app that I'm using for tests. Here my code to send a MotionEvent
:
val event = MotionEvent.obtain(
0,
SystemClock.uptimeMillis(),
MotionEvent.ACTION_MOVE,
sThumbLX.toFloat() / 32000, sThumbLY.toFloat() / 32000 * -1,
1.0f,
1.0f,
0,
1.0f,
1.0f,
3,
0
)
event.source = InputDevice.SOURCE_JOYSTICK
Timber.v("new thumb event send %s", event.toString())
instrumentation.sendPointerSync(event)
event.recycle()
Instead, when I use the real gamepad, I get many events like the following:
2019-09-12 15:29:50.576 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [1.0 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1.0, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=1437191, downTime=0, deviceId=5, source=0x1000010 }]
2019-09-12 15:29:50.576 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Down] - code [22] event [{KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_RIGHT, scanCode=0, metaState=0, flags=0x400, repeatCount=0, eventTime=1437191, downTime=1437191, deviceId=5, source=0x1000010 }}]
2019-09-12 15:29:51.016 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [0.85882366 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=0.85882366, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=1437627, downTime=0, deviceId=5, source=0x1000010 }]
2019-09-12 15:29:51.036 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [0.27058828 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=0.27058828, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=3, eventTime=1437651, downTime=0, deviceId=5, source=0x1000010 }]
2019-09-12 15:29:51.036 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Up] - code [22] event [{KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DPAD_RIGHT, scanCode=0, metaState=0, flags=0x400, repeatCount=0, eventTime=1437651, downTime=1437651, deviceId=5, source=0x1000010 }}]
2019-09-12 15:29:51.056 4555-4555/it.eng.ds.poc.gamepadtestapplication V/EVENT: Event type [Motion] - x [0.003921628 y [-0.003921509] event [MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=0.003921628, y[0]=-0.003921509, toolType[0]=TOOL_TYPE_UNKNOWN, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=1437663, downTime=0, deviceId=5, source=0x1000010 }]
I'm following the right way or this is not the correct way to do it?
Thanks.