[Techtalk] Question about XGrabKey

artur artur.miler.810 at student.lu.se
Mon Dec 8 04:23:59 EST 2003

Hi to all;

Can anyone help me with XLib and XGrabKey function?

I have created an override_redirect window and want it to get some 
input. Mouse works just as normal, but the keyboard seems to be a bit 
problem.I I have register it with EnterNotifyMask so I can test when 
mouse pointer gets in/out of that window. In main loop I have following 

/case EnterNotify:
    XGrabKey(dpy, XK_q,0,win, True, GrabModeAsync, GrabModeAsync);
    printf("keycode: %c\n",XK_q);
/*     XSetInputFocus(dpy, win, RevertToParent, CurrentTime); */
case LeaveNotify:
/*     XGetInputFocus(dpy,&cur_focus,&ret_focus); */

/I have tested with both XGetInputFocus and XGrabKeyboard to get input 
in that window and both of them works. I can "grabb" and "ungrabb" 
keyboard with success in those 2 cases above. Problem is that with both 
focus & keyboard versions /all/  keyboard input goes to that window; 
which I don't want. I just want to use one key (for example 'q') to 
close the window, and rest to still be available to wm.

I think XGrabKey should be for that purpose but for some reason it 
doesn't work. When I start my window it doesn't "grabb" the key at all.
Does anybode know why it does work with XGrabKeyboard but not with 
XGrabKey?? I have read man page and seen quite a few examples on the net 
and most of them use XGrabKey like I did above.  What can be misstake? 
How should I use it to get it work?

By the way; the function has an argument for modifiers; if I don't want 
to use any - is it ok to pass 0 (or None) as argument. In man page it 
says nothing about using it without modifiers (only with modifiers- 
smart a?). I have also tested to register it with control key but it 
still didn't work (the window never got the 'q' as input).

More information about the Techtalk mailing list