All Input Events

Author: Alex Vanyo

A Modifier that tracks all input events, and calls the block lambda passed to it every time an input is received.

// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
/**
 * A [Modifier] that tracks all input, and calls [block] every time input is received.
 */
private fun Modifier.notifyInput(block: () -> Unit): Modifier =
    composed {
        val currentBlock by rememberUpdatedState(block)
        pointerInput(Unit) {
            while (currentCoroutineContext().isActive) {
                awaitPointerEventScope {
                    awaitPointerEvent(PointerEventPass.Initial)
                    currentBlock()
                }
            }
        }
    }

Have a project you'd like to submit? Fill this form, will ya!

If you like this snippet, you might also like:

Maker OS is an all-in-one productivity system for developers

I built Maker OS to track, manage & organize my life. Now you can do it too!