Python Xlib.error.BadAccess on attempting to access X11 shortkeys
Asked Answered
D

1

6

I'm trying to catch shortkeys (e.g. Ctrl Alt direction key) in a Python script running in the background. When I try to do this, I'm encountering errors. My code is as follows:

import Xlib
import Xlib.display

def process_event(event):

    keycode = event.detail
    if event.type == Xlib.X.KeyPress:
        print(keycode)

def main():

    # current display
    display    = Xlib.display.Display()
    rootWindow = display.screen().root

    # catch keypress events
    rootWindow.change_attributes(event_mask = Xlib.X.KeyPressMask)

    keys = [10, 11]

    for keycode in keys:
        rootWindow.grab_key(
            keycode,
            Xlib.X.Mod1Mask, #Xlib.X.AnyModifier,
            1,
            Xlib.X.GrabModeAsync,
            Xlib.X.GrabModeAsync
        )

    while True:
        event = rootWindow.display.next_event()
        process_event(event)

if __name__ == '__main__':
    main()

The errors I'm getting are as follows:

<class 'Xlib.protocol.request.QueryExtension'>
X protocol error:
<class 'Xlib.error.BadAccess'>: code = 10, resource_id = 157, sequence_number = 10, major_opcode = 33, minor_opcode = 0
X protocol error:
<class 'Xlib.error.BadAccess'>: code = 10, resource_id = 157, sequence_number = 11, major_opcode = 33, minor_opcode = 0
Dextran answered 22/9, 2015 at 19:47 Comment(1)
I get all this when trying to run autokey-gtk on Ubuntu 14.04.Frogmouth
L
0

This error is happened because of duplicate key binding on xlib. Most probably other application already using them in key binding.

I just tested key binding similar as yours works on my Unity and Kde but wont work on my xfce4 with similar error.

X protocol error:
<class 'Xlib.error.BadAccess'>: code = 10, resource_id = 483, sequence_number 
= 10, major_opcode = 33, minor_opcode = 0
X protocol error:
<class 'Xlib.error.BadAccess'>: code = 10, resource_id = 483, sequence_number 
= 11, major_opcode = 33, minor_opcode = 0

Changing the keycode or edit other application keybind (if you know which application) will fix the issue. Sometimes it is application from your desktop environment.

For others, same error could also happens because of wrong keycode. In the question it uses keycode 10 and 11 (correct value).

Laktasic answered 1/3, 2021 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.