rhx / SwiftGtk

A Swift wrapper around gtk-3.x and gtk-4.x that is largely auto-generated from gobject-introspection
https://rhx.github.io/SwiftGtk/
BSD 2-Clause "Simplified" License
317 stars 26 forks source link

More idiomatic way to work with events #24

Closed lschmierer closed 4 years ago

lschmierer commented 4 years ago

How can I access event struct fields without accessing _ptr?

connectKey(signal: "key_press_event") { _, event in
    let event = event._ptr.pointee
    debugPrint(event.keyval)
}
rhx commented 4 years ago

Swift doesn't support C-style unions such as GdkEvent, so there is no corresponding Swift wrapper with more idiomatic properties at the moment. I'll have a think if there is a way to teach gir2swift some magic to work around this ...

rhx commented 4 years ago

With SwiftGtk 3.24.17 and gir2swift 10, you can now access GdkEvent fields directly from Swift. Also, you probably want to use onKey or onKeyPress instead of connectKey, e.g.:

        widget.onKeyPress { widget, event in
            let key = event.keyval
            let hw = event.hardwareKeycode
            // ...
        }

        canvas.onKey(event: .keyPressEvent) { widget, event in
            let key = event.keyval
            let hw = event.hardwareKeycode
            // ...
        }