Hi @ebassi,
I create a Gtk.Box
and put a Gtk.Button
in it. When clicking the button, I change the button to a New accelerator
button, and add a Gtk.EventControllerKey
to this button. Afterwards I except when I press any key, the Gtk.EventControllerKey
can catch it. Code like the below lines:
const rendererAccelBox = new Gtk.Box({
spacing: 6,
margin_top: 6,
margin_bottom: 6,
margin_start: 6,
margin_end: 6,
});
const addAccelButton = new Gtk.Button({
label: 'Add accelerator',
});
addAccelButton.connect('clicked', () => {
const newAccelButton = new Gtk.Button({
label: 'New accelerator...',
});
const eventControllerKey = new Gtk.EventControllerKey();
newAccelButton.add_controller(eventControllerKey);
// newAccelButton.grab_focus();
newAccelButton.set_state_flags(Gtk.StateFlags.GTK_STATE_FLAG_ACTIVE, true);
// Error! Gtk does not have expose gtk_grab_add?
Gtk.grab_add(newAccelButton);
eventControllerKey.connect('key-pressed', (_widget, keyval, keycode, state) => {
log('key-pressed');
});
rendererAccelBox.append(newAccelButton);
});
When I click the New accelerator
button, and press any key, the
Gtk.EventControllerKey
can catch it.
I’ve read the source code of gtkcellrendereraccel.c#gtk_cell_editable_widget_new
, it uses gtk_grab_add
to grab the keyboard and pointer events.
My question is that how can I make the New accelerator
button grab the keyboard and pointer events? I can’t find suitable APIs in the Gtk – 4.0
If the code / implement is not right, please let me know. Thank you very much.