如何创建和触发 Gdk.EventKey
How to create and fire a Gdk.EventKey
我有一个 Gtk.Window
覆盖 OnKeyPressEvent
protected override bool OnKeyPressEvent(Gdk.EventKey evt)
{
Console.WriteLine("Captured key " + evt.Key);
return base.OnKeyPressEvent(evt);
}
所以我能够捕捉到 "real" 键盘操作。
现在我想创建一个类似于虚拟键盘的东西(由简单的按钮组成)。但是我没有找到任何关于触发自己的关键事件的信息。
谁能告诉我如何使用 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?
}
}
我有一个 Gtk.Window
覆盖 OnKeyPressEvent
protected override bool OnKeyPressEvent(Gdk.EventKey evt)
{
Console.WriteLine("Captured key " + evt.Key);
return base.OnKeyPressEvent(evt);
}
所以我能够捕捉到 "real" 键盘操作。
现在我想创建一个类似于虚拟键盘的东西(由简单的按钮组成)。但是我没有找到任何关于触发自己的关键事件的信息。
谁能告诉我如何使用 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?
}
}