In preferences of my gnome shell extension I have a keyboard shortcut editor. When editing a shortcut, it needs to catch all keys, including fn/media keys. On Gtk 3 I can achieve it using Gdk.Seat.grab() on the default seat. However, I can’t find any replacement for it in Gtk/Gdk 4, and the migration guide tells to stop using grabs. So how do I catch fn/media keys?
If your player supports MPRIS it should receive media keys from gsd-media-keys automatically through those methods, without needing to grab.
Alternatively, if using MPD or something similar, you could configure “Custom Shortcuts” to run a command line tool such as mpc when pressing the media keys. But even that may be unnecessary, you may have better results with a bridge like mpdris2.
If I’m misunderstanding and you only need it for a shortcut editor, you likely don’t need a grab there, simply making the dialog modal will cause it to capture keyboard focus for that application.
My own keyboard has special keys like “Explorer”, “Mail”. And I need Gdk.Seat.grab() to catch them in the shortcut editor - otherwise the shell handles them, and the application (preferences dialog) gets no events at all.