Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left/right/middle mouse click/drag #129

Open
Cybermaxke opened this issue Nov 20, 2020 · 7 comments
Open

Left/right/middle mouse click/drag #129

Cybermaxke opened this issue Nov 20, 2020 · 7 comments
Assignees
Labels
desktop enhancement New feature or request input Touch, mouse, keyboard input related

Comments

@Cybermaxke
Copy link

Cybermaxke commented Nov 20, 2020

Right now its impossible to know which mouse button was used, I would like to be able to assign different behaviors to each kind of click and drag.

@Cybermaxke Cybermaxke changed the title Left/right/middle mouse click Left/right/middle mouse click/drag Nov 24, 2020
@igordmn igordmn added the enhancement New feature or request label Nov 24, 2020
@olonho olonho added the input Touch, mouse, keyboard input related label Dec 8, 2020
@prepor
Copy link
Contributor

prepor commented Feb 23, 2021

We added mouseEvent property for PointerEvent on Desktop. You can use it as a workaround while we are working at better and (I hope) common API for Desktop and Android. I think the easiest way to get PointerEvent is by directly using new pointerInput API. For example:

private suspend fun AwaitPointerEventScope.awaitEventFirstDown(): PointerEvent {
    var event: PointerEvent
    do {
        event = awaitPointerEvent()
    } while (
        !event.changes.all { it.changedToDown() }
    )
    return event
}


var lastEvent by remember { mutableStateOf<MouseEvent?>(null) }
Text(
    modifier = Modifier.pointerInput(Unit) {
        forEachGesture {
            awaitPointerEventScope {
                lastEvent = awaitEventFirstDown().also {
                    it.changes.forEach { it.consumeDownChange() }
                }.mouseEvent
            }
        }
    },
    text = "Custom mouse event button: ${lastEvent?.button}"
)

@atoktoto
Copy link

One addition to this workaround that someone might find useful. You can user this in addition to normal .clickable {} modifier:

.clickable {
    if (lastEvent?.button == 3) {
        model.onClick()
    } else {
        dropdownMenuExpanded.value = true
    }
}
.pointerInput(Unit) {
    forEachGesture {
        awaitPointerEventScope {
            lastEvent = awaitEventFirstDown().mouseEvent
        }
    }
}

The order of modifiers matters here. Not sure why. My theory is that clickable consumes the events, and the events are processed from the last element first.

@akurasov
Copy link
Contributor

@prepor do we still have anything to do here?

@akurasov akurasov self-assigned this Jul 15, 2021
@ejektaflex
Copy link

Whenever I'm inside of this code, inside of pointerInput:

forEachGesture {
  awaitPointerEventScope {
    val down = awaitFirstDown()
    drag(down.id) {
      println(currentEvent.mouseEvent?.button)
    }
  }
}

The printed result I get is 0, no matter which button I've clicked.

@akurasov akurasov removed their assignment Sep 17, 2021
@ejektaflex
Copy link

ejektaflex commented Oct 4, 2021

Furthermore, there's no way of handling mouseUp. I really would like to handle a mouse release over an element, but that doesn't seem to be possible (only mouse down). This is so that I can implement drag & drop, releasing the mouse on a different element.

@zsmb13
Copy link

zsmb13 commented Nov 13, 2023

Seems like this is also missing on the web.

The info is there in the native pointer events, but it's really inconvenient to get to.

Disclaimer: this modifier code here might conflict with other clicks/gestures, use with caution, the event consuming part could probably be written in a nicer way.

Modifier.pointerInput(Unit) {
    awaitPointerEventScope {
        while (true) {
            val event = awaitPointerEvent()
            val nativeEvent = event.nativeEvent
            if (nativeEvent is SkikoPointerEvent) {
                if (nativeEvent.button == SkikoMouseButtons.RIGHT && nativeEvent.kind == SkikoPointerEventKind.UP) {
                    event.changes.forEach { it.consume() } // or similar, would need to check if this works nicely with other registered listeners
                    println("Right click!")
                }
            }
        }
    }
}

@okushnikov
Copy link

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
desktop enhancement New feature or request input Touch, mouse, keyboard input related
Projects
None yet
Development

No branches or pull requests

9 participants