/// <summary> /// Fires a key press event to a widget. This is not something that should be used very often, /// but it provides a way to store a value of a cell being edited in a grid when the user closes the grid /// (e.g., by selecting a different view). /// I haven't been able to find any better way to do it. /// It's placed in this unit because there may be uses in other contexts. /// </summary> /// <param name="widget"></param> /// <param name="key"></param> public static void SendKeyEvent(Gtk.Widget widget, Gdk.Key key) { uint keyval = (uint)key; Gdk.Window window = widget.GdkWindow; Gdk.KeymapKey[] keymap = Gdk.Keymap.Default.GetEntriesForKeyval(keyval); EventKeyStruct native = new EventKeyStruct(); native.type = Gdk.EventType.KeyPress; native.window = window.Handle; native.send_event = 1; native.state = (uint)Gdk.EventMask.KeyPressMask; native.keyval = keyval; native.length = 0; native.str = null; native.hardware_keycode = (ushort)keymap[0].Keycode; native.group = (byte)keymap[0].Group; IntPtr ptr = GLib.Marshaller.StructureToPtrAlloc(native); try { Gdk.EventKey evnt = new Gdk.EventKey(ptr); Gtk.Main.DoEvent(evnt); } finally { GLib.Marshaller.Free(ptr); } }
/// <summary> /// Fires a key press event to a widget. This is not something that should be used very often, /// but it provides a way to store a value of a cell being edited in a grid when the user closes the grid /// (e.g., by selecting a different view). /// I haven't been able to find any better way to do it. /// It's placed in this unit because there may be uses in other contexts. /// </summary> /// <param name="widget"></param> /// <param name="key"></param> public static void SendKeyEvent(Gtk.Widget widget, Gdk.Key key) { uint keyval = (uint)key; Gdk.Window window = widget.GdkWindow; Gdk.KeymapKey[] keymap = Gdk.Keymap.Default.GetEntriesForKeyval(keyval); EventKeyStruct native = new EventKeyStruct(); native.type = Gdk.EventType.KeyPress; native.window = window.Handle; native.send_event = 1; native.state = (uint)Gdk.EventMask.KeyPressMask; native.keyval = keyval; native.length = 0; native.str = null; native.hardware_keycode = (ushort)keymap[0].Keycode; native.group = (byte)keymap[0].Group; IntPtr ptr = GLib.Marshaller.StructureToPtrAlloc(native); try { Gdk.EventKey evnt = new Gdk.EventKey(ptr); Gdk.EventHelper.Put(evnt); // We need to process the event, or we won't be able // to safely free the unmanaged pointer // Using DoEvent for this fails on the Mac while (GLib.MainContext.Iteration()) { ; } // Gtk.Main.DoEvent(evnt); } finally { GLib.Marshaller.Free(ptr); } }