How do I get accessibility service to react to keyboard button press

61 Views Asked by At

I'm trying to create a custom Android accessibility service for simulating screen taps when I press different keys on a Bluetooth keyboard.

I'm quite new to Android app development outside of Flutter, so I'm suspecting that issue lies somewhere in the manifest or accessibility configuration xml, rather than in the Kotlin code.

Starting from an empty activity in Android Studio, I added the following service to AndroidManifest.xml:

        <service
            android:name=".KeyRemapAccessibilityService"
            android:exported="false"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service_config" />
        </service>

accessibility_service_config.xml looks like this:

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:canPerformGestures="true"
    android:canRequestFilterKeyEvents="true"
    android:canRetrieveWindowContent="true"
    android:notificationTimeout="0"
    android:description="@string/service_description" />

Here's the main KeyRemapAccessibilityService.kt code:

package com.example.keyboard_remap_service;
import android.accessibilityservice.AccessibilityService
import android.view.KeyEvent
import android.view.accessibility.AccessibilityEvent
import android.util.Log

class KeyRemapAccessibilityService : AccessibilityService() {

    override fun onServiceConnected() {
        super.onServiceConnected()
        Log.i("AccessibilityService", "Service connected")
        // Configure your service here if needed
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        // Handle accessibility events if needed
        Log.i("AccessibilityService", "Accessibility event")
    }

    override fun onInterrupt() {
        // Handle service interrupt
        Log.i("AccessibilityService", "Interrupt")
    }

    override fun onKeyEvent(event: KeyEvent?): Boolean {
        Log.i("AccessibilityService", "Key Event Received: ${event?.keyCode}")
        return super.onKeyEvent(event)
    }
}

Android Studio manages to compile and launch the app and I get a "Greeting" screen on my phone and a bunch of "Accessibility event" logs when I enable the service and press on stuff. However, I don't see any "Key Event Received" logs when I press on the keyboard.

I've found an app that does what I want, its source code is here and I've managed to do what I want by cloning it and changing this function, but I'd like to understand how I can change my code to work the way I want it to rather than continue to work with a really big codebase and changing tiny things here and there.

0

There are 0 best solutions below