How to detect if the source of a scroll or mouse event is a trackpad or a mouse in JavaFX? -
this same question this one javafx instead of java swing:
i want know if scroll event generated trackpad or mouse in javafx.
according documentation on scrollevent
there subtle difference in handling scroll events mouse , trackpad.
when scrolling produced touch gesture (such dragging finger on touch screen), surrounded
scroll_started
,scroll_finished
events.
having in mind can track scroll_started
, scroll_finished
events , modify scroll_event
handler in between these 2 boundaries. trackpad can send scroll_event
s after scroll_finished
being sent (scrolling inertia) can check out event.isinertia()
method filter these events.
due probable bug in javafx in rare cases scroll_event
s may occur after scroll_finished
event.isinertia() == false
(if scroll on trackpad many many times). possible workaround track timestamp of last scroll_finished event , ignore these "ghost" events within short time period after timestamp.
example code:
long lastfinishedscrollingtime; boolean trackpadscrolling; node.setonscroll(event -> { long timediff = system.currenttimemillis() - lastfinishedscrollingtime; boolean ghostevent = timediff < 1000; // saw 500-700ms ghost events if (trackpadscrolling || event.isinertia() || ghostevent) { // trackpad scrolling } else { // mouse scrolling } }); node.setonscrollstarted(event -> { trackpadscrolling = true; }); node.setonscrollfinished(event -> { trackpadscrolling = false; lastfinishedscrollingtime = system.currenttimemillis(); });
Comments
Post a Comment