FYNE List-Widget "list.OnSelected": check if CTRL-Key is pressed or not #5813
-
|
Hello community, in my app i have a List.Widget containing Labels. For hours and hours i try to get it working, that the Callback OnSelected could check, if the CTRL-Key is pressed or not. If it is pressed, confirm action 1, if not - confirm action 2. I've searched on the board for similar discussions - and i found some which are close enough to my problem i think. I have also looked ad the fyne_demo-code - especially on advanced.go - but i can't figure out, how i can use the "OnKeyUp" and "OnKeyDown" funcs. But i can't get it up and running - my currently now last version of the code (see below) won't compile with this errors :( This is my code: package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2/container"
// "fyne.io/fyne/v2/driver/desktop"
)
var ctrltapped bool
type mylist struct {
widget.List
}
func newMylist(length func() int, createItem func() fyne.CanvasObject, updateItem func(id widget.ListItemID, item fyne.CanvasObject)) *mylist {
l := &mylist{}
l.Length = length
l.CreateItem = createItem
l.UpdateItem = updateItem
l.ExtendBaseWidget(l)
return l
}
func (ml *mylist) SetOnKeyDown(func(ev *fyne.KeyEvent) {
if (ev.Name == "LeftControl" {
ctrltapped = true
}
}){}
func (ml *mylist) SetOnKeyUp(func(ev *fyne.KeyEvent) {
if (ev.Name == "LeftControl" {
ctrltapped = false
}
}){}
func (ml *mylist) OnKeyDown() func(*fyne.KeyEvent) {
return nil
}
func (ml *mylist) OnKeyUp() func(*fyne.KeyEvent) {
return nil
}
func main() {
fmt.Println("starting gui")
ctrltapped = false
app := app.New()
win := app.NewWindow("this is MRT...")
data := []string{"eins", "zwei", "drei"}
l := newMylist(
func() int {
return len(data)
},
func() fyne.CanvasObject {
return widget.NewLabel("template")
},
func(id widget.ListItemID, o fyne.CanvasObject) {
o.(*widget.Label).SetText(data[id])
},
)
l.OnSelected = func(id widget.ListItemID) {
// if (CTRL is HOLD DOWN) {
if ctrltapped {
fmt.Println("with ctrl")
} else {
fmt.Println("without CTRL down")
}
}
mainbox := container.NewBorder(nil, nil, nil, nil,
l,
)
win.SetContent(mainbox)
win.CenterOnScreen()
win.Resize(fyne.NewSize(1024, 760))
win.ShowAndRun()
}I've already read the "Extending Widgets"-Page on the fyne-homepage and thought i'ver understood, how it works. For Buttons and Labels i got some extensions to work - but not the solution for my "List"-Widget. I'm confused and don't know what to do now - can somebody please help me or give me a hint what i have to do? Bests |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
|
try this -> |
Beta Was this translation helpful? Give feedback.
-
|
You don't need any extended or custom widgets and you don't need to register key handlers - just use the if desk, ok := driver.(desktop.Driver); ok {
modifiers := desk.CurrentKeyModifiers()
fmt.Println("Current modifiers:", modifiers)
} |
Beta Was this translation helpful? Give feedback.
-
|
Hope it will be usefull for someone else :) package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
func main() {
fmt.Println("starting gui")
app := app.New()
win := app.NewWindow("this is MRT...")
desk, ok := app.Driver().(desktop.Driver)
if !ok {
panic("could not get desktop-driver for app!")
}
data := []string{"eins", "zwei", "drei"}
l := widget.NewList(
func() int {
return len(data)
},
func() fyne.CanvasObject {
return widget.NewLabel("template")
},
func(id widget.ListItemID, o fyne.CanvasObject) {
o.(*widget.Label).SetText(data[id])
},
)
l.OnSelected = func(id widget.ListItemID) {
modifiers := desk.CurrentKeyModifiers()
fmt.Println("Current Modifiers-Value:", modifiers)
}
mainbox := container.NewBorder(nil, nil, nil, nil,
l,
)
win.SetContent(mainbox)
win.CenterOnScreen()
win.Resize(fyne.NewSize(1024, 760))
win.ShowAndRun()
}bests |
Beta Was this translation helpful? Give feedback.
In the code above
driveris a variable that hits your app driver.You could replace it with
myapp.Driver()(assuming your app instance is a variable called myapp).