在绘图区域GTK#鼠标事件鼠标、区域、事件、GTK

2023-09-04 08:16:01 作者:我是光棍我可耻

我有一个,我想收到的鼠标事件绘图区。 从教程我已经找到了关键pressEvent也将赶上鼠标事件。 然而,对于C中的处理程序如下$ C $永远不会被调用。

I have a DrawingArea which I would like to received mouse events. From the tutorials I have found that the KeyPressEvent will also catch mouse events. However for the following code the handler is never called.

static void Main ()
{
    Application.Init ();
    Gtk.Window w = new Gtk.Window ("");

    DrawingArea a = new CairoGraphic ();
    a.KeyPressEvent += KeyPressHandler;
    w.Add(a);

    w.Resize (500, 500);
    w.DeleteEvent += close_window;
    w.ShowAll ();

    Application.Run ();
}

private static void KeyPressHandler(object sender, KeyPressEventArgs args)
{
    Console.WriteLine("key press event");   
}

我已经尝试了一堆东西从阅读不同的论坛和教程,包括:

I have tried a bunch of things from reading different forums and tutorials including:

添加事件盒的窗口和把绘图区在事件框和订阅密钥pressEvent为事件盒。 (没有工作)

Adding a EventBox to the windows and putting the DrawingArea in the event box and subscribing to the KeyPressEvent for the EventBox. (didn't work)

ADDEVENTS调用((INT)Gdk.EventMask.AllEventsMask);在任何及所有部件

Calling AddEvents((int)Gdk.EventMask.AllEventsMask); on any and all widgets

我确实发现订阅的Windows键pressEvent没有给我的键盘事件,但没有鼠标点击事件。

I did find that subscribing to the Windows KeyPressEvent did give me keyboard events but not mouse click events.

在单文档中所有相关的网页给我的错误,让我有点卡住

All the relevant pages in the mono docs give me errors so I'm a bit stuck

推荐答案

您应该还记得,一个事件屏蔽应该被添加到您的绘图区:

You should also remember that an event mask should be added to your DrawingArea:

a.AddEvents ((int) 
            (EventMask.ButtonPressMask    
            |EventMask.ButtonReleaseMask    
            |EventMask.KeyPressMask    
            |EventMask.PointerMotionMask));

因此​​,最终的code看起来应该是:

So your final code should look like that:

class MainClass
{
    static void Main ()
    {
        Application.Init ();
        Gtk.Window w = new Gtk.Window ("");

        DrawingArea a = new DrawingArea ();
        a.AddEvents ((int) EventMask.ButtonPressMask);
        a.ButtonPressEvent += delegate(object o, ButtonPressEventArgs args) {
            Console.WriteLine("Button Pressed");
        };

        w.Add(a);

        w.Resize (500, 500);
        w.DeleteEvent += close_window;
        w.ShowAll ();

        Application.Run ();
    }

    static void close_window(object o, DeleteEventArgs args) {
        Application.Quit();
        return;
    }
}