GTK[mm]4: how to get all “raw” touch & mouse events?

New replay for this
https://discourse.gnome.org/t/gtk-mm-4-how-to-get-all-raw-touch-mouse-events

My trials examples like this work, but I am not expert. This code can replace “keyboard_event/simple” examplewindow.cc with mouse events.

#include "examplewindow.h"
#include <iostream>

ExampleWindow::ExampleWindow()
{
  set_title("Keyboard: Alt, Shift, Ctrl, Meta, Mouse move, Mouse click, Mouse scroll Events");
  m_container.set_margin(10);
  set_child(m_container);

  // Radio buttons:
  m_first.set_label("First (Alt+a)");
  m_second.set_label("Second (Alt+b)");

  m_second.set_group(m_first);
  m_first.set_active();

  // Main Container:
  m_container.set_orientation(Gtk::Orientation::HORIZONTAL);
  m_container.append(m_first);
  m_container.append(m_second);

  //add_events (GDK_SCROLL_SMOOTH );

  // Events.
  auto controller = Gtk::EventControllerKey::create();
  controller->signal_key_pressed().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_window_key_pressed), false);
  add_controller(controller);


  auto controller1 = Gtk::EventControllerMotion::create();
  controller1->signal_motion().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_Mouse_Motion), false);
  add_controller(controller1);

  auto controller2 = Gtk::EventControllerKey::create();
  controller2->signal_modifiers().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_Modifiers), false);
  add_controller(controller2);

    auto controller3 = Gtk::EventControllerScroll::create();
    controller3->set_flags(Gtk::EventControllerScroll::Flags::VERTICAL);
      controller3->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
    controller3->signal_scroll().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_scroll), false);
  add_controller(controller3);

  auto mouse = Gtk::GestureClick::create();
  mouse->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
  mouse->set_button(GDK_BUTTON_PRIMARY);
  mouse->signal_pressed().connect(sigc::mem_fun(*this, &ExampleWindow::on_mb1pressed), false);
  add_controller(mouse);

  auto mouse1 = Gtk::GestureClick::create();
  mouse1->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
  mouse1->set_button(GDK_BUTTON_PRIMARY);
  mouse1->signal_released().connect(sigc::mem_fun(*this, &ExampleWindow::on_mb1released), false);
  add_controller(mouse1);

    auto mouse2 = Gtk::GestureClick::create();
  mouse2->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
  mouse2->set_button(GDK_BUTTON_MIDDLE);
  mouse2->signal_pressed().connect(sigc::mem_fun(*this, &ExampleWindow::on_mb2pressed), false);
  add_controller(mouse2);

  auto mouse3 = Gtk::GestureClick::create();
  mouse3->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
  mouse3->set_button(GDK_BUTTON_MIDDLE);
  mouse3->signal_released().connect(sigc::mem_fun(*this, &ExampleWindow::on_mb2released), false);
  add_controller(mouse3);

    auto mouse4 = Gtk::GestureClick::create();
  mouse4->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
  mouse4->set_button(GDK_BUTTON_SECONDARY);
  mouse4->signal_pressed().connect(sigc::mem_fun(*this, &ExampleWindow::on_mb3pressed), false);
  add_controller(mouse4);

  auto mouse5 = Gtk::GestureClick::create();
  mouse5->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
  mouse5->set_button(GDK_BUTTON_SECONDARY);
  mouse5->signal_released().connect(sigc::mem_fun(*this, &ExampleWindow::on_mb3released), false);
  add_controller(mouse5);
}

bool ExampleWindow::on_scroll(double dx, double dy)
{
    std::cout << "on_scroll: "  << dx << ", "<< dy << std::endl;
return true;
 }

bool ExampleWindow::on_Modifiers(Gdk::ModifierType state)
{
  if ((state & Gdk::ModifierType::SHIFT_MASK)  == Gdk::ModifierType::SHIFT_MASK)
  {
    std::cout << "_SHIFT" << std::endl;
        return true;
  }
  if ((state & Gdk::ModifierType::CONTROL_MASK)  == Gdk::ModifierType::CONTROL_MASK)
  {
    std::cout << "_CTRL" << std::endl;
        return true;
  }
  if ((state & Gdk::ModifierType::ALT_MASK)  == Gdk::ModifierType::ALT_MASK)
  {
    std::cout << "_ALT" << std::endl;
        return true;
  }
  if ((state & Gdk::ModifierType::META_MASK)  == Gdk::ModifierType::META_MASK)
  {
    std::cout << "_META" << std::endl;
        return true;
  }

return false;
}

void ExampleWindow::on_mb1pressed(int n_press, double x, double y)
{
std::cout << "on_mb1Pressed: " << n_press << ", " << x << ", "<< y << std::endl;

}

void ExampleWindow::on_mb1released(int n_press, double x, double y)
{
std::cout << "on_mb1Released: " << n_press << ", " << x << ", "<< y << std::endl;
}

void ExampleWindow::on_mb2pressed(int n_press, double x, double y)
{
std::cout << "on_mb2Pressed: " << n_press << ", " << x << ", "<< y << std::endl;
}

void ExampleWindow::on_mb2released(int n_press, double x, double y)
{
std::cout << "on_mb2Released: " << n_press << ", " << x << ", "<< y << std::endl;
}

void ExampleWindow::on_mb3pressed(int n_press, double x, double y)
{
std::cout << "on_mb3Pressed: " << n_press << ", " << x << ", "<< y << std::endl;
}

void ExampleWindow::on_mb3released(int n_press, double x, double y)
{
std::cout << "on_mb3Released: " << n_press << ", " << x << ", "<< y << std::endl;
}


void ExampleWindow::on_Mouse_Motion(double x, double y)
{
std::cout << "on_Mouse_Motion: "  << x << ", "<< y << std::endl;
}

bool ExampleWindow::on_window_key_pressed(guint keyval, guint, Gdk::ModifierType state)
{
  //Gdk::ModifierType::ALT_MASK -> the 'Alt' key(mask)
  //GDK_KEY_1 -> the '1' key
  //GDK_KEY_2 -> the '2' key

  //select the first radio button, when we press alt + 1
  if((keyval == GDK_KEY_a) &&
    (state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
  {
    m_first.set_active();
    //returning true, cancels the propagation of the event
    return true;
  }
  else if((keyval == GDK_KEY_b) &&
    (state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
  {
    //and the second radio button, when we press alt + 2
    m_second.set_active();
    return true;
  }
  else if(keyval == GDK_KEY_Escape)
  {
    //close the window, when the 'esc' key is pressed
    set_visible(false);
    return true;
  }


  //the event has not been handled
  return false;
}

ExampleWindow::~ExampleWindow()
{
}

The title of this Discourse entry ends with a question mark, but I don’t understand
what the question is.

Gtk::EventControllerLegacy, discussed in
https://discourse.gnome.org/t/gtk-mm-4-how-to-get-all-raw-touch-mouse-events,
exists since gtkmm 4.4.

The code that you show is similar or identical to the one you showed in issue
gtkmm-documentation#23.
As a result of that issue, I added gtkmm-documentation commit
Add example and description of mouse events.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.