Home > Article > Backend Development > Press and hold button in golang fyne
php Xiaobian Yuzai will introduce to you the skills and usage of pressing and holding buttons in Golang Fyne. Fyne is a lightweight framework for building cross-platform graphical user interfaces, and button press and hold is an important feature of it. By pressing and holding the button, the user can perform a series of continuous operations, such as dragging, drawing, etc. This article will explain in detail how to implement the button press function in Golang Fyne, and provide practical code examples and operation steps to help readers quickly master this technique. Whether you are a beginner or an experienced developer, you can gain practical knowledge and skills from this article and improve your development capabilities in Golang Fyne. Stay tuned for follow-up content!
Using the fyne GUI framework, you can easily build a simple GUI with basic functionality. I can't find a way to detect when the user presses and holds a button for a certain period of time. I can only detect if the button is clicked. Is this possible?
The widgets in fyne have the most likely functionality. When a widget does not provide the required functionality, it can be extended. An example of icons being clickable can be seen here. In this example, the interface fyne/v2.Tappable is being implemented which also contains an icon, which results in a clickable icon.
In order to make the button maintainable, we need to implement an interface that can register mousedown and mouseup events. This interface is fyne/v2/driver/desktop.Mouseable.
This is what it looks like:
package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/driver/desktop" "log" ) type holdableButton struct { widget.Button } func newHoldableButton(label string) *holdableButton { button := &holdableButton{} button.ExtendBaseWidget(button) button.Text=label return button } func (h *holdableButton) MouseDown(*desktop.MouseEvent){ log.Println("down") } func (h *holdableButton) MouseUp(*desktop.MouseEvent){ log.Println("up") } func main() { a := app.New() w := a.NewWindow("Holdable") w.SetContent(newHoldableButton("Button")) w.ShowAndRun() }
It should be noted that the Mouseable interface will not appear by name. You only need to import the driver package to reference desktop.MouseEvent. But as is the case in golang, the interface is implemented by creating two methods, MouseUp and MouseDown, which need to exist even if you only "need" one, otherwise none of these will work.
Thanks to andy.xyz for pointing me in the right direction.
The above is the detailed content of Press and hold button in golang fyne. For more information, please follow other related articles on the PHP Chinese website!