Event Based

  Edit on GitHub

LittleKt provides an event based approach of handling input. It provides events for keyboard, mouse, and gamepads. All we need to do is implement an interface and add it to the Context.

Input Processor

Context contains a list of InputProcessor objects that are processed in insert order. We can add as many input processors as we want to it and even remove them when we are done with it.

The InputProcessor interface:

interface InputProcessor {

    fun keyDown(key: Key): Boolean = false

    fun keyUp(key: Key): Boolean = false

    fun keyRepeat(key: Key): Boolean = false

    fun charTyped(character: Char): Boolean = false

    fun touchDown(screenX: Float, screenY: Float, pointer: Pointer): Boolean = false

    fun touchUp(screenX: Float, screenY: Float, pointer: Pointer): Boolean = false

    fun touchDragged(screenX: Float, screenY: Float, pointer: Pointer): Boolean = false

    fun mouseMoved(screenX: Float, screenY: Float): Boolean = false

    fun scrolled(amountX: Float, amountY: Float): Boolean = false

    fun gamepadButtonPressed(button: GameButton, pressure: Float, gamepad: Int): Boolean = false

    fun gamepadButtonReleased(button: GameButton, gamepad: Int): Boolean = false

    fun gamepadJoystickMoved(stick: GameStick, xAxis: Float, yAxis: Float, gamepad: Int): Boolean = false

    fun gamepadTriggerChanged(button: GameButton, pressure: Float, gamepad: Int): Boolean = false
}

Keyboard events:

  • keyDown: called when a key is pressed down.
  • keyUp: called when a key is released.
  • keyRepeat: called when a key is pressed down and held.
  • charTyped: called when a unicode character is generated by the keyboard input. Can be used to implement text fields and other UI elements.

Mouse/Touch events:

  • touchDown: called when a pointer was pressed / touched the screen.
  • touchUp: called when a pointer is released / lifted from the screen.
  • touchDragged: called when a pointer is being dragged while being pressed / touching the screen.
  • mouseMoved: called when the mouse is moved over the screen. Only occurs on desktop.
  • scrolled: called when the scroll wheel of the mouse was triggered. Returns -1 or 1 for the delta values depending on the scroll direction. Only occurs on the desktop.

Gamepad events:

  • gamepadButtonPressed: called when a button on a gamepad is pressed.
  • gamepadButtonReleased: called when a button on a gamepad is released.
  • gamepadJoystickMoved: called when a joystick on a gamepad is moved. Reports the stick and the current axis values of that joystick.
  • gamepadTriggerChanged: called when a trigger axis is changed. This is mainly to handle trigger buttons such as the LT and RT buttons on an Xbox controller.

Using an InputProcessorBuilder with syntactic sugar

class MyGame(context: Context) : ContextListener(context) {

    // override any of the methods in InputProcessor that we want to actually make use of.
    override suspend fun Context.start() {
        val processor = input.inputProcessor { // creates a new InputProcessor and adds it the the current conext
            onKeyDown { key -> logger.info { "Key $:key is down!" } }
        }

        onDispose {
            removeInputProcessor(processor) // we don't have to do this if we are closing the application
        }
    }
}

Implementing the InputProcessor interface

class MyGame(context: Context) : ContextListener(context), InputProcessor {

    init {
        context.addInputProcessor(this)
    }


    // override any of the methods in InputProcessor that we want to actually make use of.
    override suspend fun Context.start() {
        onDispose {
            removeInputProcessor(this) // we don't have to do this if we are closing the application
        }
    }
}