0

so i'm developing a desktop application using compose desktop and I need to detect horizontal scroll.

here is what I tried already:

  .onPointerEvent(PointerEventType.Scroll) { println(it.changes.first().scrollDelta)}

the problem is i can only get vertical scrolls! how can I detect horizontal scroll event?

by the way by horizontal scroll I mean scrolling with touchpad

MohammadBaqer
  • 766
  • 6
  • 32

1 Answers1

1

To get horizontal scroll changes use x axis in scrollDelta:

.onPointerEvent(PointerEventType.Scroll) { 
   val scrollDelta = it.changes.fold(Offset.Zero) { acc, c -> acc + c.scrollDelta }
   println(scrollDelta.x) 
}

Note: it is needed to fold all changes, otherwise some scroll deltas can be skipped.

Also, you can use horizontalScroll modifier to make a component horizontally scrollable automatically.

val scrollState = remember { ScrollState(initial = 0) }
Box(modifier = Modifier.size(100.dp).horizontalScroll(scrollState)) {
  Text("Scrollable".repeat(20))
}

LaunchedEffect(scrollState.value) {
  println(scrollState.value)
}
  • with your first solution I wasnt abale to detect horizontal scrolling. and with your second solution I want to get raw events of scrolling so I dont have a list to scroll – MohammadBaqer Mar 29 '23 at 00:00
  • Can you please provide a small example, where this scrolling is not detected, may be some other components consume scroll event? – Nikolai Rykunov Apr 05 '23 at 13:48
  • you can read it in this issue https://github.com/JetBrains/compose-multiplatform/issues/2956#event-8919406084 – MohammadBaqer Apr 06 '23 at 00:32