我想让一个小的Python程序在后台运行来监视某些快捷键的按下(例如Ctrl
Alt
direction key
)。这个想法是这些快捷键可以触发各种操作。
这是尝试的开始,但老实说我不知道我在做什么:
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()