How to grab and hide cursor using gtkmm?

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I'm trying to write an application using gtkmm, and I want it to hide the mouse cursor when it has focus. So the first step I tried is I hide mouse cursor when the cursor is on top of my window, which is successful. But to prevent the mouse from showing when it is moved outside of my window, I then restrict the mouse movement by constantly warp the mouse cursor back to the center of my drawing area.

To do this I need to know the window position and the size of the window, which are easy to get using Gdk::Window::get_position and Gdk::Window::get_size. However, if the window is moved, get_position will not return the updated position and thus my cursor will to frozen at a wrong position!

So what are the alternatives to achieve the effect that I want here?

回答1:

Just grab the pointer.

//pass all events to window apart LEAVE_NOTIFY_MASK  MainWindow->set_events(GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK | GDK_PROPERTY_CHANGE_MASK | GDK_VISIBILITY_NOTIFY_MASK | GDK_PROXIMITY_IN_MASK | GDK_PROXIMITY_OUT_MASK | GDK_SUBSTRUCTURE_MASK | GDK_SCROLL_MASK | GDK_TOUCH_MASK | GDK_SMOOTH_SCROLL_MASK | GDK_TOUCHPAD_GESTURE_MASK); MainWindow->set_modal(true); auto display = MainWindow->get_display(); auto window = MainWindow->get_window(); auto grabSuccess = display->get_default_seat()->grab(window, Gdk::SEAT_CAPABILITY_ALL, true); if(grabSuccess != Gdk::GRAB_SUCCESS) {     std::clog<<"grab failed: "<<grabSuccess<<std::endl; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!