gpt4 book ai didi

python - 我如何从 linux 的后台检测 python 中的组合键?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:38:10 26 4
gpt4 key购买 nike

我想做的是让一个脚本在后台运行,当我按下一个组合键(例如 Fn + w)时,另一个脚本被执行。

如果这在 python 中很难做到,我可以在另一个中做到,或者如果有人知道另一种方法来做到这一点。

我正在使用带有 XFCE 的 Ubuntu 11.10。

最佳答案

存在一个名为 python-xlib 的 python 库。你可以捕获 key 并在你的系统上做一些事情 http://python-xlib.sourceforge.net/

这是一个示例代码,服务器通过网络接收键码并按下它:

import Xlib.display
import Xlib.X
import Xlib.XK
import Xlib.protocol.event
import Xlib.ext.xtest
import socket, sys


s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
keycode = None
try:
s.bind(("", 49999))
i=0
display = Xlib.display.Display()

while True:
daten, addr = s.recvfrom(1024)
if (daten != 'ende'):
keycode = daten
if keycode == '111' or keycode == '116':
print '%d up/down' % i
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, int(keycode) )
display.sync()
if keycode == '117':
# A
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, int(
keycode) )
display.sync()
if keycode == '118':
# Y
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, int(keycode) )
display.sync()
if keycode == '119':
# SPACE
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, int(keycode) )
display.sync()
if daten == 'ende':
if keycode != None:
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, int(keycode) )
keycode = None
display.sync()
daten = None
continue

finally:
s.close()

关于python - 我如何从 linux 的后台检测 python 中的组合键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12752898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com