如何检测背景中的按键组合使用python的Linux和Windows?
例如,
当检测到
Ctrl+v
时,在后台执行doThis()
当检测到
Tab
时,在后台执行doThat()
如果你正在使用python tkinter,有文件菜单。 那么下面的代码可能会帮助你。
from Tkinter import * import sys import Tkinter class App(Tkinter.Tk): def __init__(self): Tkinter.Tk.__init__(self) menubar = Tkinter.Menu(self) fileMenu = Tkinter.Menu(menubar, tearoff=False) menubar.add_cascade(label="File", underline=0, menu=fileMenu) fileMenu.add_command(label="doThat", underline=1, command=quit, accelerator="Ctrl+v") fileMenu.add_command(label="doThis", underline=1, command=quit, accelerator="Tab") self.config(menu=menubar) self.bind_all("<Control-v>", self.doThat) self.bind_all("<Tab>", self.doThis) def doThat(self, event): print("Control v is pressed ...") def doThis(self, event): print("Tab is pressed...") if __name__ == "__main__": app = App() app.mainloop()
在Windows上,这可以使用pyhook完成
在Ubuntu的我做了这个pyxhook的帮助
编辑:Windows和Linux的另一个令人敬畏的库 – 键盘