When using mouse clicks, there are two main differences between Windows and Gtk # that you should keep in mind:
- Gtk # does not offer double-click signals ("Events" in Windows lingo), but only single 'click' signals. However, the Gdk library implements both double-click and triple-click with its EventButton class!
- Gtk # distinguishes between Widgets (or "Controls" on Windows lingo) and "Containers" (no direct mapping on Windows). Most widgets hosted on the Gtk # form will NOT receive mouse click events. To receive a mouse event, you need to place the widget inside a specific container, such as an EventBox.
This is how you do Gtk #:
a. Add an EventBox to the form (in the example below: eventbox1). You can place it behind other widgets or, because it does not appear, unless you specifically select it (or change its background color). You can place the widget inside the EventBox, but you are limited to only one widget, which will also receive the shape and size of the EventBox.
C. Add a ButtonPressEvent signal from the Common Widget Signals signals to this EventBox (in the following example: OnEventbox1ButtonPressEvent)
Each time the mouse button (on the left, in the middle or in the center or combination) is called inside the EventBox, it calls this event, and the OnEventbox1ButtonPressEvent () function will be called. If you need to identify the button that was pressed when processing this event, use the uint value in: args.Event.Button usually "1" will be the left mouse button, "2" the center button and "3" the right button ('2' can be also by pressing the left and right buttons).
By the way, mouse movement events (without clicking a button) are not sent by default. Therefore, if you need to feel them, you will need to add PointMotionMask, as well as in the first code example below.
Here is a sample ButtonPress event handler code (EventBox name is "eventbox1") that captures a double-click event using the EventButton class:
// The following line is may not be needed but is here to show how to do it eventbox1.GdkWindow.Events = eventbox1.GdkWindow.Events | Gdk.EventMask.ButtonPressMask; protected void OnEventbox1ButtonPressEvent (object o, ButtonPressEventArgs args) { if( ((Gdk.EventButton)args.Event).Type == Gdk.EventType.TwoButtonPress) System.Media.SystemSounds.Beep.Play (); // Play a sound only if this is a double-click event }
The order of the received events (in the case of a double click):
- Gdk.EventType.ButtonPress
- Gdk.EventType.ButtonRelease
- Gdk.EventType.ButtonPress
- Gdk.EventType.TwoButtonPress
- Gdk.EventType.ButtonRelease
Hope this helps!
source share