最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How do I track low level events for a mouse right click Press and Release on a Jetpack Compose UI element? - Stack Ove

programmeradmin0浏览0评论

I am building an app on Android that is intended to be used with a mouse. The UI of my app is in Jetpack Compose. I am familiar with the pointerInput modifier and all the wonderful wrappers put in our disposal to handle well known finger gestures. In my case it doesn't help me since I want to receive each low level event for every button press and release. I've implemented a method that I call when the PointerEvent is received in the pointerInput modifier but my problem is that I can't find a way to know which button was pressed or release without needing to keep track of the button states over time, which seems like a terrible way to go about it and would open the door for all sorts of out of sync problems. I'm able to do exactly what I want with the old set of Android APIs for MotionEvents but not with this new system.

Here's what my modifier code looks like:

Box(
    modifier =
        Modifier.pointerInput(Unit) {
              awaitPointerEventScope {
                while (true) {
                  val event = awaitPointerEvent()
                  viewModel.handlePointerEvent(event, size.width, size.height)
                }
              }
            }
            .onKeyEvent { keyEvent ->
              viewModel.handleKeyEvent(keyEvent)
              false
            }) {
          [...]
        }

I am able to tell that a DOWN or UP event just happened by comparing the PointerEvent.type to PointerEventType.Press or PointerEventType.Release, but I can't tell which button this action was taken on.

PointerEvent has a list of changes that will show me all the changes that occurred in this event. The problem is that it will say if isPressed or not but not which button underwent this change.

PointerInputChange(
  id: PointerId,
  uptimeMillis: Long,
  position: Offset,
  pressed: Boolean,
  pressure: Float,
  previousUptimeMillis: Long,
  previousPosition: Offset,
  previousPressed: Boolean,
  isInitiallyConsumed: Boolean,
  type: PointerType = PointerType.Touch,
  scrollDelta: Offset = Offset.Zero
)

PointerEvent also has a list of buttons that will show me the current state of all the input buttons but this doesn't tell me which one changed in this event. Only that these buttons are currently in this state. Conveniently there is a set of extension methods (isPrimaryPressed, isSecondaryPressed, isTertiaryPressed) that will tell you when each of the buttons are in the "Pressed" state but that doesn't tell you if the button just entered that state or if it was already in that state before.

I also tried looking at the values reported by indexOfLastPressed() but this also only cares about the pressed state and not released.

I find myself incapable of detecting a right click UP event unless I keep track of the state of all the buttons in my code which seems like a step back from the pre-existing MotionEvent API which would tell you very clearly with each event what button the event corresponded to.

Since the new APIs are based on the MotionEvents under the hood I'm hoping there's a way to access this information.

This question is different from How to manage mouse right/center click with Jetpack Compose? because there the person only cares about detecting which button was pressed. In my case I want to get both Pressed and Released even if you hold left click and then do a right click before releasing the left click. My question is more about understanding how we get to the low level events of up and down actions on a per button and per event basis.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论