c# - Mono 和 Gtk 中的全局热键#

标签 c# mono x11 gtk#

我正在尝试使用 Mono 在 Linux 中使用全局热键。我找到了 XGrabKeyXUngrabKey 的签名,但我似乎无法让它们工作。每当我尝试调用 XGrabKey 时,应用程序都会崩溃并显示 SIGSEGV。

这是我目前所拥有的:

using System;
using Gtk;
using System.Runtime.InteropServices;

namespace GTKTest
{
    class MainClass
    {
        const int GrabModeAsync = 1;

        public static void Main(string[] args)
        {
            Application.Init();

            MainWindow win = new MainWindow();
            win.Show();

            // Crashes here
            XGrabKey(
             win.Display.Handle,
             (int)Gdk.Key.A,
             (uint)KeyMasks.ShiftMask,
             win.Handle,
             true,
             GrabModeAsync,
             GrabModeAsync);

            Application.Run();

            XUngrabKey(
             win.Display.Handle,
             (int)Gdk.Key.A,
             (uint)KeyMasks.ShiftMask,
             win.Handle);
        }


        [DllImport("libX11")]
        internal static extern int XGrabKey(
         IntPtr display,
         int keycode,
         uint modifiers,
         IntPtr grab_window,
         bool owner_events,
         int pointer_mode,
         int keyboard_mode);

        [DllImport("libX11")]
        internal static extern int XUngrabKey(
         IntPtr display,
         int keycode,
         uint modifiers,
         IntPtr grab_window);
    }

    public enum KeyMasks
    {
        ShiftMask = (1 << 0),
        LockMask = (1 << 1),
        ControlMask = (1 << 2),
        Mod1Mask = (1 << 3),
        Mod2Mask = (1 << 4),
        Mod3Mask = (1 << 5),
        Mod4Mask = (1 << 6),
        Mod5Mask = (1 << 7)
    }
}

有人有 XGrabKey 的工作示例吗?

谢谢!

最佳答案

好吧,我终于在托管代码中找到了一个可行的解决方案。 SIGSEGV 发生是因为我混淆了非托管 Gdk 对象的句柄和它们对应的 X11 对象的句柄。感谢 Paul 的回答,我找到了一个不受管理的全局热键示例,并熟悉了它的工作原理。然后我编写了自己的非托管测试程序来找出我需要做什么,而不必处理任何托管特性。成功后,我创建了一个托管解决方案。

这是托管解决方案:

public class X11Hotkey
{
    private const int KeyPress = 2;
    private const int GrabModeAsync = 1;
    private Gdk.Key key;
    private Gdk.ModifierType modifiers;
    private int keycode;

    public X11Hotkey(Gdk.Key key, Gdk.ModifierType modifiers)
    {
        this.key = key;
        this.modifiers = modifiers;

        Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
        IntPtr xDisplay = GetXDisplay(rootWin);
        this.keycode = XKeysymToKeycode(xDisplay, (int)this.key);
        rootWin.AddFilter(new Gdk.FilterFunc(FilterFunction));
    }

    public event EventHandler Pressed;

    public void Register()
    {
        Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
        IntPtr xDisplay = GetXDisplay(rootWin);

        XGrabKey(
                 xDisplay,
                 this.keycode,
                 (uint)this.modifiers,
                 GetXWindow(rootWin),
                 false,
                 GrabModeAsync,
                 GrabModeAsync);     
    }

    public void Unregister()
    {
        Gdk.Window rootWin = Gdk.Global.DefaultRootWindow;
        IntPtr xDisplay = GetXDisplay(rootWin);

        XUngrabKey(
                 xDisplay,
                 this.keycode,
                 (uint)this.modifiers,
                 GetXWindow(rootWin));
    }

    private Gdk.FilterReturn FilterFunction(IntPtr xEvent, Gdk.Event evnt)
    {
        XKeyEvent xKeyEvent = (XKeyEvent)Marshal.PtrToStructure(
            xEvent, 
            typeof(XKeyEvent));

        if (xKeyEvent.type == KeyPress)
        {
            if (xKeyEvent.keycode == this.keycode 
                && xKeyEvent.state == (uint)this.modifiers)
            {
                this.OnPressed(EventArgs.Empty);
            }
        }

        return Gdk.FilterReturn.Continue;
    }

    protected virtual void OnPressed(EventArgs e)
    {
        EventHandler handler = this.Pressed;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    private static IntPtr GetXWindow(Gdk.Window window)
    {
        return gdk_x11_drawable_get_xid(window.Handle);
    }

    private static IntPtr GetXDisplay(Gdk.Window window)
    {
        return gdk_x11_drawable_get_xdisplay(
            gdk_x11_window_get_drawable_impl(window.Handle));
    }

    [DllImport("libgtk-x11-2.0")]
    private static extern IntPtr gdk_x11_drawable_get_xid(IntPtr gdkWindow); 

    [DllImport("libgtk-x11-2.0")]
    private static extern IntPtr gdk_x11_drawable_get_xdisplay(IntPtr gdkDrawable);

    [DllImport("libgtk-x11-2.0")]
    private static extern IntPtr gdk_x11_window_get_drawable_impl(IntPtr gdkWindow);

    [DllImport("libX11")]
    private static extern int XKeysymToKeycode(IntPtr display, int key);

    [DllImport("libX11")]
    private static extern int XGrabKey(
        IntPtr display, 
        int keycode, 
        uint modifiers, 
        IntPtr grab_window, 
        bool owner_events, 
        int pointer_mode, 
        int keyboard_mode);

    [DllImport("libX11")]
    private static extern int XUngrabKey(
        IntPtr display, 
        int keycode, 
        uint modifiers, 
        IntPtr grab_window);

#if BUILD_FOR_32_BIT_X11        

    [StructLayout(LayoutKind.Sequential)]
    internal struct XKeyEvent
    {
        public short type;
        public uint serial;
        public short send_event;
        public IntPtr display;
        public uint window;
        public uint root;
        public uint subwindow;
        public uint time;
        public int x, y;
        public int x_root, y_root;
        public uint state;
        public uint keycode;
        public short same_screen;
    }       
#elif BUILD_FOR_64_BIT_X11

    [StructLayout(LayoutKind.Sequential)]
    internal struct XKeyEvent
    {
        public int type;
        public ulong serial;
        public int send_event;
        public IntPtr display;
        public ulong window;
        public ulong root;
        public ulong subwindow;
        public ulong time;
        public int x, y;
        public int x_root, y_root;
        public uint state;
        public uint keycode;
        public int same_screen;
    }
#endif      

}

这是测试程序:

public static void Main (string[] args)
{
    Application.Init();

    X11Hotkey hotkey = new X11Hotkey(Gdk.Key.A, Gdk.ModifierType.ControlMask);
    hotkey.Pressed += HotkeyPressed;;
    hotkey.Register();

    Application.Run();

    hotkey.Unregister();
}

private static void HotkeyPressed(object sender, EventArgs e)
{
    Console.WriteLine("Hotkey Pressed!");
}

我不确定 XKeyEvent 结构在具有不同大小的 C intlong 的其他系统上如何表现,所以该解决方案是否适用于所有系统还有待观察。

编辑: 由于底层 C 类型大小的不同性质,看起来这个解决方案不会像我担心的那样独立于体系结构。 libgtkhotkey看起来很有希望避免使用托管程序集部署和编译自定义非托管库。

注意:现在您需要根据操作系统的字长显式定义 BUILD_FOR_32_BIT_X11BUILD_FOR_64_BIT_X11

关于c# - Mono 和 Gtk 中的全局热键#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2349958/

相关文章:

linux - X Window System Window Hierarchy探索程序

opengl - 打开全屏 OpenGL 窗口

java - 一个询问您要使用哪个网络浏览器的程序

c# - 'System.Web.Http.GlobalConfiguration' 的类型初始值设定项抛出异常

c# - nameof 与泛型

c# - .Net 和/或单声道是否有等效的 ccache 或 distcc

c# - 如何从 c# 中的 ViewModelHelper 关闭窗口(XAML)?

c# - Azure Functions - Host.json 不适用于 Azure 应用程序设置

c# - 如何使用 Mono 从 Linux 中的网络摄像头捕获?

python - 将 HTML HEX 颜色或 RGB 元组转换为 X11 颜色