To create an InputMethodService, you can refer to this official document.
First, add service in the Manifest.xml:
<service android:name=".IMService"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod"/>
</intent-filter>
<meta-data
android:name="android.view.im"
android:resource="@xml/method"/>
</service>
Next, method.xml is need to create under res/xml/ folder:
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:isDefault="false"
android:settingsActivity=".MainActivity">
<subtype
android:icon="@mipmap/ic_launcher"
android:imeSubtypeLocale="en_US"
android:imeSubtypeMode="keyboard"
android:label="@string/app_name"/>
</input-method>
Layout of the keyboard is not needed in this case.
Then, create the class IMService implementing the InputMethodService.
The following code is written in Kotlin (for example):
class IMService: InputMethodService() {
companion object {
private const val TAG = "IMService"
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if(event.action == KeyEvent.ACTION_DOWN) {
if (event.source and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD) {
// process key down event here
return true // return true if you want the key event to be filtered
}
}
return super.onKeyDown(keyCode, event)
}
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
if(event.action == KeyEvent.ACTION_UP) {
if (event.source and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD) {
// process key up event here
return true
}
}
return super.onKeyUp(keyCode, event)
}
override fun onGenericMotionEvent(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_MOVE) {
if (event.source and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK) {
// process motion event here
return true
}
}
return super.onGenericMotionEvent(event)
}
}
The service lifecycle starts after you enabled and changed the default input method in Settings/../Language and keyboard/Keyboard list and default.