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