`Gtk.EventControllerKey::modifiers` signals emitted on modifier key press/release reflect previous state, not new state
imports.gi.versions.Gdk = "4.0";
imports.gi.versions.Gtk = "4.0";
const { Gdk, GLib, Gtk } = imports.gi;
Gtk.init();
let controller = new Gtk.EventControllerKey();
controller.connect('modifiers', (_, mods) => {
let shift = (mods & Gdk.ModifierType.SHIFT_MASK) != 0;
print("shift:", shift);
});
let win = new Gtk.Window();
win.add_controller(controller);
win.show();
let loop = new GLib.MainLoop(null, false);
loop.run();
With this script, pressing and releasing shift
, then pressing a
results in:
shift: false
shift: true
shift: false
When handling the GDK_KEY_PRESS
for the shift key, the Event Controller emits modifiers
, but it seems the modifier state set at the time still has it not set. Then GDK_KEY_RELEASE
results in it reporting shift as active, until any other key is pressed.
Presumably this behavior is not desired. It doesn't seem to make much sense to have a modifiers
signal that behaves this way.
This seems to be the same on X or Wayland. Presumably this event should be generated in response to wl_keyboard::modifiers
on Wayland? (Though that would involve a new Gdk event type...) Not sure how that works on X.