[Gtk-sharp-list] GTK# mouse event in drawing area

Andy Selvig ajselvig at gmail.com
Tue Mar 17 22:59:54 EDT 2009


Hi-

The KeyPressEvent only catches keyboard presses. In order to catch the
mouse, you have to use these events: ButtonPressEvent, ButtonReleaseEvent,
and MotionNotifyEvent. They handle mouse button press, mouse button release,
and mouse motion, respectively.

And yes, the simplest thing to do is use an EventBox. You can subclass
EventBox and do your drawing and mouse handling inside of it:

public class MyWidget : EventBox
{
    public MyWidget() : base()
    {
        // initialize
    }

    protected override bool OnButtonPressEvent(EventButton evnt)
    {
        // handle mouse button presses
    }

    protected override bool OnButtonReleaseEvent(EventButton evnt)
    {
        // handle mouse button releases
    }

    protected override bool OnMotionNotifyEvent(EventMotion evnt)
    {
        // handle mouse motion
    }

    protected override bool OnKeyPressEvent(EventKey evnt)
    {
        // handle keyboard press
    }

    protected override bool OnExposeEvent(EventExpose evt)
    {
        using (Context cr = Gdk.CairoHelper.Create(evt.Window))
        {
            // draw the widget
        }
    }
}

This should be enough to get you started.

-Andy


On Mon, Mar 16, 2009 at 8:13 AM, trampster <trampster at gmail.com> wrote:

>
>  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
> --
> View this message in context:
> http://www.nabble.com/GTK--mouse-event-in-drawing-area-tp22537881p22537881.html
> Sent from the Mono - Gtk# mailing list archive at Nabble.com.
>
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20090317/5bf211aa/attachment.html 


More information about the Gtk-sharp-list mailing list