Re: how to get key press event in goocanvasmm

I don't know anything about goocanvasmm, but I know a lot about gtkmm and glibmm. As far as I can see your on_key_press_event() should be called, provided that m_canvas has the keyboard focus. If a child of m_canvas has the keyboard focus, on_key_press_event() should be called if the child does not handle the signal. If the child handles the signal, and returns true from its signal handler, there's no hope for you.

You can read about event signals in the gtkmm tutorial, and

If on_key_press_event() is not called there's another possibility. Instead of overriding the default signal handler, connect another signal handler. (It won't do any difference if one of m_canvas's children handles the signal.)

bool on_canvas_key_press_event(GdkEventKey* event)
{ ....... }
  signal_key_press_event().connect(sigc::mem_fun(*this, &MyCanvas::
on_canvas_key_press_event), false);

Note false in connect().

I recommend that you replace Gtk::Main by Gtk::Application, unless you use a very old version of gtkmm. Gtk::Main has been deprecated since 2012.


On 2020-02-14 18:51, Klaus Rudolph wrote:
Is it possible to receive key press events in canvas?

The following signal handler is not called inside goocanvas. It compiles fine but it is not called if a key is pressed.


Glib::RefPtr<Goocanvas::Item> root;

class MyCanvas: public Goocanvas::Canvas
    double scale = 1;

    bool on_key_press_event( GdkEventKey* event ) override
        std::cout << "on_key_press_event" << std::endl;
        return true;


int main(int argc, char* argv[])
    Gtk::Main app(&argc, &argv);
    Goocanvas::init("example", "0.1", argc, argv);

    Gtk::Window win;

    MyCanvas m_canvas;
    m_canvas.set_size_request(640, 480);
    m_canvas.set_bounds(0, 0, 800, 800);
    root = m_canvas.get_root_item();


    Glib::RefPtr<Goocanvas::Ellipse> outer = Goocanvas::Ellipse::create( 100,100,20,20);
    outer->property_line_width() = 5;
    outer->property_stroke_color() = "red";
    root->add_child( outer );

    win.add( m_canvas);

    return 0;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]