https://kotlinlang.org logo
Title
c

Christian Babsek

06/23/2021, 2:50 PM
Hello, is there a way to react on mouse scrolling events? (like scroll up, scroll down with the mouse wheel)? Thank you very much 🙂
o

olonho

06/23/2021, 3:26 PM
are you interested in scroll events or mouse wheel events? did you check https://github.com/JetBrains/compose-jb/tree/master/tutorials/Desktop_Components#scrollbars?
c

Christian Babsek

06/23/2021, 3:27 PM
In my case, it would be really just the event of the button itself, I wanted to make an image preview of a pdf page to be somehow "scrollable" by turning the mouse wheel (scroll between the pages, the event would replace the image).
o

olonho

06/23/2021, 3:28 PM
See
Modifier.mouseScrollFilter
then
👀 1
c

Christian Babsek

06/23/2021, 3:29 PM
I will check, thanks already 🙂
Thank you very much, it worked perfectly 🙂
For anyone who is interested in my solution:
@Composable
fun Modifier.onVerticalScroll(
    onScrollUp: () -> Unit = {},
    onScrollDown: () -> Unit = {}
) = mouseScrollFilter(
    onMouseScroll = { event, _ ->
        if (event.orientation == MouseScrollOrientation.Vertical) {
            when (val delta = event.delta) {
                is MouseScrollUnit.Line -> if (delta.value < 0) onScrollUp() else onScrollDown()
                is MouseScrollUnit.Page -> if (delta.value < 0) onScrollUp() else onScrollDown()
            }
        }
        false
    }
)
👍🏼 1
👍🏻 1
👍 5