Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

An Android plugin for the Flutter package "gamepads". Has now been integrated into the gamepads monorepo.

License

Notifications You must be signed in to change notification settings

markvideon/gamepads_android

Repository files navigation

gamepads_android

Note: This project has been migrated into the gamepads monorepo.

Getting Started

This plugin functions by forwarding input events from your application's Acitivty. Therefore you will need to modify your activity to ensure this happens. Your activity should look something like...

package [YOUR_PACKAGE_NAME]

import android.hardware.input.InputManager
import android.os.Handler
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
import io.flutter.embedding.android.FlutterActivity
import dev.markvideon.gamepads_android.GamepadsCompatibleActivity

class MainActivity: FlutterActivity(), GamepadsCompatibleActivity {
    var keyListener: ((KeyEvent) -> Boolean)? = null
    var motionListener: ((MotionEvent) -> Boolean)? = null

    override fun dispatchGenericMotionEvent(motionEvent: MotionEvent): Boolean {
        motionListener?.invoke(motionEvent)
        return true
    }

    override fun dispatchKeyEvent(keyEvent: KeyEvent): Boolean {
        keyListener?.invoke(keyEvent)
        return true
    }

    override fun isGamepadsInputDevice(device: InputDevice): Boolean {
        return device.sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD
            || device.sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
    }

    override fun registerInputDeviceListener(listener: InputManager.InputDeviceListener, handler: Handler?) {
        val inputManager = getSystemService(INPUT_SERVICE) as InputManager
        inputManager.registerInputDeviceListener(listener, null)
    }

    override fun registerKeyEventHandler(handler: (KeyEvent) -> Boolean) {
        keyListener = handler
    }

    override fun registerMotionEventHandler(handler: (MotionEvent) -> Boolean) {
        motionListener = handler
    }
}

About

An Android plugin for the Flutter package "gamepads". Has now been integrated into the gamepads monorepo.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages