Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GTK# mouse event in drawing area

Tags:

c#

.net

mono

gtk#

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:

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)

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

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

like image 723
trampster Avatar asked Mar 16 '09 12:03

trampster


People also ask

Is GTK C or C++?

GTK is written in C but has been designed to support a wide range of languages such as Python, JavaScript, C++, Rust and many more.

What is difference between GTK and GTK+?

GTK/GTK+ and GTK2 are different versions of the same API. GTK is an old, deprecated version, GTK2 is the previous one, GTK+ 3/GTK3 is the current version. GTK+ is the correct name of the old API, but most people just call it GTK. The C++ bindings for GTK+ are part of the project GTKmm.

Does Ubuntu use GTK?

GTK-Based Desktops and SoftwareTake GNOME, the default option on such prominent and well-established Linux OSes as Ubuntu, Fedora, and Debian.

What is GTK theme?

Themes allows you to customize the color and shape of widget by assigning colors and/or pixmap to GTK application. This can give you a completely new look and feel to you application.

What is GTK in C?

GTK is a widget toolkit. Each user interface created by GTK consists of widgets. This is implemented in C using GObject, an object-oriented framework for C. Widgets are organized in a hierarchy. The window widget is the main container.

What does GTK stand for Linux?

GTK is commonly and incorrectly thought to stand for "GNOME ToolKit", but is actually stands for "GIMP ToolKit" because it was first created to design an user interface for GIMP. GTK is an object-oriented toolkit written in C (GTK itself is not a language).


1 Answers

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));

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;
    }
}
like image 61
Piotr Zurek Avatar answered Sep 17 '22 15:09

Piotr Zurek