9.4 Grabbing the Keyboard and Pointer

There are times when a program might want to bypass the normal keyboard or pointer event propagation path in order to get input independent of the position of the pointer. This is the purpose of grabbing the keyboard and pointer. There are routines to grab the keyboard (XGrabKeyboard()) or the pointer (XGrabPointer()), or to arrange that they become grabbed when a certain combination of keys and/or buttons is pressed (XGrabButton(), XGrabKey()). There are corresponding calls to ungrab (XUngrabButton(), XUngrabKey(), XUngrabKeyboard(), XUngrabPointer()), and there is one call to change the characteristics of a pointer grab (XChangeActivePointerGrab()).

One of the most common situations where grabbing takes place is with button events. Most applications want both a ButtonPress and a ButtonRelease, so that they can compare the two positions. Since this is such a common desire, the server automatically grabs the pointer between the ButtonPress and ButtonRelease if both are selected, so that you do not have to make an explicit grab.

One reason for grabbing a device is so that you can handle a series of events contiguously without fear of intervening events. But when you grab a device, no other application can receive input from that device. Therefore, it is something to do only when absolutely necessary.

The routines that grab take several arguments that tailor the input response in these ways:

  • When the pointer is grabbed, the cursor may be confined ...

Get XLIB Programming Manual, Rel. 5, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.