2

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?

Xavier_Ex
  • 8,432
  • 11
  • 39
  • 55
  • 1
    How is the user going to focus another application if their cursor is hidden outside your window? – ptomato Jul 12 '12 at 15:10
  • @ptomato thanks for the reply, the user can use "alt+tab" to switch focus outside of the application. Switching focus is not a big deal, I can have a keyboard event to disable the cursor grab. An FPS game can be taken as an example, when in game the cursor is hidden and always centered in the window. When you enter the game menu or so the mouse would reappear... I hope that clears up my question :) – Xavier_Ex Jul 12 '12 at 16:53
  • 1
    I have to agree with ptomato, this sounds like bad UI. You shouldn't stop a user from leaving your app's window. Video games usually (usually) only do this in fullscreen mode, to keep the mouse from going to a different screen or what not. Other than that, I seriously can't see why a windowed app would need this. – Christian Smith Jul 13 '12 at 17:46
  • @senshikaze Thanks for the feedback. For the game example, imagine you are playing Diablo 3 in windowed mode instead of full screen, you would want to restrict the mouse from moving outside the window to prevent misclick. In my case, I am writing a game in which the mouse movement of the user is taken as input. Thus the reason I want to disallowed the cursor from moving outside the window while the gameplay is in progress. – Xavier_Ex Jul 14 '12 at 22:10

1 Answers1

0

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;
}
pan-mroku
  • 803
  • 6
  • 17