[GH-ISSUE #667] Example of using mouse events #489

Closed
opened 2026-03-04 01:05:25 +03:00 by kerem · 5 comments
Owner

Originally created by @githubedu on GitHub (Nov 5, 2021).
Original GitHub issue: https://github.com/rivo/tview/issues/667

Hi,

I'm having issues to understand the usage of MouseActions.

In my case I want to call a function when "MouseLeftDown" is triggered or check that it has been triggered, when clicking in a button.

Could someone give me some basic advice where and how to use it?

Thank you

Originally created by @githubedu on GitHub (Nov 5, 2021). Original GitHub issue: https://github.com/rivo/tview/issues/667 Hi, I'm having issues to understand the usage of MouseActions. In my case I want to call a function when "MouseLeftDown" is triggered or check that it has been triggered, when clicking in a button. Could someone give me some basic advice where and how to use it? Thank you
kerem closed this issue 2026-03-04 01:05:25 +03:00
Author
Owner

@rivo commented on GitHub (Nov 8, 2021):

If you just want to be notified when a button was clicked, that's what Button.SetSelectedFunc() is for. This will be called also when the user activates the button with the keyboard.

If you specifically want to check for a left mouse click (which is quite uncommon, but I don't know your use case), you use button.SetMouseCapture(). Your function will be called for any mouse events directed at the button. You can then check if the action argument (type MouseAction) is MouseLeftDown and do whatever you need to do.

Let me know if this helps.

<!-- gh-comment-id:963242159 --> @rivo commented on GitHub (Nov 8, 2021): If you just want to be notified when a button was clicked, that's what [`Button.SetSelectedFunc()`](https://pkg.go.dev/github.com/rivo/tview#Button.SetSelectedFunc) is for. This will be called also when the user activates the button with the keyboard. If you specifically want to check for a left mouse click (which is quite uncommon, but I don't know your use case), you use [`button.SetMouseCapture()`](https://pkg.go.dev/github.com/rivo/tview#Box.SetMouseCapture). Your function will be called for any mouse events directed at the button. You can then check if the `action` argument (type `MouseAction`) is `MouseLeftDown` and do whatever you need to do. Let me know if this helps.
Author
Owner

@githubedu commented on GitHub (Nov 16, 2021):

Thank you Rivo,
The SetMouseCapture is the one I was looking for.

I'm using to control motor axis. The user push a button on a touch screen, it activate on MouseLeftDown and keep active the motor until MouseLeftUp.
Best regards.

<!-- gh-comment-id:970626877 --> @githubedu commented on GitHub (Nov 16, 2021): Thank you Rivo, The `SetMouseCapture` is the one I was looking for. I'm using to control motor axis. The user push a button on a touch screen, it activate on `MouseLeftDown` and keep active the motor until `MouseLeftUp`. Best regards.
Author
Owner

@markusressel commented on GitHub (Sep 13, 2023):

I want to leave a comment here mentioning that app.EnableMouse() is required for any callbacks set via SetMouseCapture() to be triggered.

Took me a while to figure out 😉

<!-- gh-comment-id:1718298476 --> @markusressel commented on GitHub (Sep 13, 2023): I want to leave a comment here mentioning that `app.EnableMouse()` is required for any callbacks set via `SetMouseCapture()` to be triggered. Took me a while to figure out :wink:
Author
Owner

@mikayzo commented on GitHub (Feb 15, 2024):

I want to leave a comment here mentioning that app.EnableMouse() is required for any callbacks set via SetMouseCapture() to be triggered.

Took me a while to figure out 😉

jesus f****** christ, took me half a day to find this f****** comment and fix the f****** mouse issues i had. bless you sir

<!-- gh-comment-id:1946409902 --> @mikayzo commented on GitHub (Feb 15, 2024): > I want to leave a comment here mentioning that `app.EnableMouse()` is required for any callbacks set via `SetMouseCapture()` to be triggered. > > Took me a while to figure out 😉 jesus f****** christ, took me half a day to find this f****** comment and fix the f****** mouse issues i had. bless you sir
Author
Owner

@rivo commented on GitHub (Apr 3, 2024):

I added some clarifications regarding the activation of mouse handling, see github.com/rivo/tview@a22293bda9.

<!-- gh-comment-id:2034786320 --> @rivo commented on GitHub (Apr 3, 2024): I added some clarifications regarding the activation of mouse handling, see https://github.com/rivo/tview/commit/a22293bda94443b8e287d94957beab835e45187b.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#489
No description provided.