Search code examples
c++positiongtkmousegtkmm3

How can I get cursor position with Gtkmm?


Which Gtkmm functions can help me get the position(x,y) of the cursor. According to this, C# has

Gdk.Display.Default.WarpPointer(Gdk.Display.DefaultScreen, 20, 20);

Windows has

GetCursorPos

Qt has

QCursor::pos()

Gtkmm has what?


Solution

  • Here you are.

    #include <stdio.h>
    #include <gtkmm.h>
    
    int main(int argc, char* argv[]) {
        Gtk::Main gtkm(argc, argv);
        Glib::RefPtr<Gdk::Display> disp = Gdk::Display::get_default();
        Glib::RefPtr<Gdk::Screen> scrn = disp->get_default_screen();
        Gdk::ModifierType mods;
        int xpos, ypos;
    
        disp->get_pointer(scrn, xpos, ypos, mods);
        printf("xpos = %d, ypos = %d\n", xpos, ypos);
        return 0;
    }