如何创建和激发Gdk.EventKey
本文关键字:Gdk EventKey 何创建 创建 | 更新日期: 2023-09-27 18:27:38
我有一个覆盖OnKeyPressEvent
的Gtk.Window
protected override bool OnKeyPressEvent(Gdk.EventKey evt)
{
Console.WriteLine("Captured key " + evt.Key);
return base.OnKeyPressEvent(evt);
}
所以我能够捕捉到"真实"的键盘动作。
现在我想创建一个类似虚拟键盘的东西(由简单的按钮组成)。但我没有发现任何关于解雇自己的关键事件的信息。
有人能告诉我如何用GTK#以编程方式触发按键,以便在我的OnKeyPressEvent
中处理该操作吗?
最后我找到了一个解决方案。
如果你知道其他解决方案(可能没有本地代码),请告诉我:)
[StructLayout(LayoutKind.Sequential)]
public struct EventKeyStruct
{
public EventType type;
public IntPtr window;
public sbyte send_event;
public uint time;
public uint state;
public uint keyval;
public uint length;
public string str;
public ushort hardware_keycode;
public byte group;
public uint is_modifier;
}
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
{
EventKey evnt = new EventKey(ptr);
EventHelper.Put(evnt);
}
finally
{
//GLib.Marshaller.Free(ptr); //comment because otherwise it crashes here?
}
}