I also couldn't find examples so here is my knowledge about it:
Mouse events (introduction):
When using mouse Gdk propagates events GDK_BUTTON_PRESS
, GDK_BUTTON_RELEASE
(and a few other). That gets translated into GtkWidget
signals like button-press-event and then into higher level ones like GtkButton
's clicked if applicable. Connecting a callback to the button-press-event
signal allows access to GdkEventButton structure. Using clicked
however frees you from keeping track whether it was a click (press & release) or only a release (during kinetic scrolling for instance).
Touch events:
Touch works a little bit different. There are 4 touch events:
GDK_TOUCH_BEGIN
A new touch event sequence has just started. This event type was added
in 3.4.
GDK_TOUCH_UPDATE
A touch event sequence has been updated. This event type was added in
3.4.
GDK_TOUCH_END
A touch event sequence has finished. This event type was added in 3.4.
GDK_TOUCH_CANCEL
A touch event sequence has been canceled. This event type was added in
3.4.
and GdkEventTouch structure uses GdkEventSequence for differentiating between fingers. It seems to me that it is simply a value (couldn't find definition in the sources) but I may be mistaken here. GtkWidget
has touch-event signal similar to button-press-event
etc that also gets translated into events like clicked
.
Sample code (using gtkmm but core aspects are the same):
#include <gtkmm.h>
#include <iostream>
int main()
{
auto app = Gtk::Application::create();
Gtk::Window window;
window.set_default_size(1024, 768);
app->signal_startup().connect([&]
{
app->add_window(window);
});
window.show();
//code works for me without adding events mask but let's be thorough
window.add_events(Gdk::TOUCH_MASK);
window.signal_touch_event().connect([&](GdkEventTouch* event)->bool
{
std::cout<<"TOUCH EVENT: ";
switch(event->type)
{
case GDK_TOUCH_BEGIN:
std::cout<<"begin ";
break;
case GDK_TOUCH_UPDATE:
std::cout<<"update ";
break;
case GDK_TOUCH_END:
std::cout<<"end ";
break;
case GDK_TOUCH_CANCEL:
std::cout<<"cancel ";
break;
default:
std::cout<<"something else ";
}
std::cout<<event->sequence<<" "
<<gdk_event_get_event_sequence((GdkEvent*)event)<<" "
<<std::endl;
return GDK_EVENT_PROPAGATE;
});
window.signal_event().connect([&](GdkEvent* event)->bool
{
std::cout<<"EVENT: "<<event->type<<std::endl;
return GDK_EVENT_PROPAGATE;
});
app->run();
return 0;
}
Touchpad events:
There are also touchpad & pad events and structures but it seems that there is no explicit handling of these on Gtk
level. It has to be done in callback for event signal with checking GdkEventType and casting it into appropriate structures.