Search code examples
pythonkeyboardkeyboard-shortcutsxlib

grabbing keyboard doesnt allow changing focus


as soon as i use display.grab_keyboard, no other window seems to know of its own focus.

with the keyboardgrab running i can select other windows and even send keyevents to them, but if this window is a text input there will be no blinking cursor.

i read something about grab_keyboard generating focusevents, but that doesnt mean it blocks all focus events, does it?

what am i not getting here?

from Xlib import X,XK
from Xlib.display import Display
import signal,sys

root = None
display = None

def main():
    # current display
    global display,root
    display = Display()
    root = display.screen().root


    root.change_attributes(event_mask = X.KeyPressMask|X.KeyReleaseMask)

    root.grab_keyboard(True, X.GrabModeAsync, X.GrabModeAsync,X.CurrentTime)

    signal.signal(signal.SIGALRM, lambda a,b:sys.exit(1))
    signal.alarm(10)

    while True:
        event = display.next_event()
        print event.type

main()

Solution

  • You are grabbing the keyboard, that means that all keyboard input will go to your program, no other window can receive keyboard input. That's the point of grabbing the keyboard.