【问题标题】:How to detect key-press combinations in background on linux & windows with python?如何使用python检测linux和windows后台的按键组合?
【发布时间】:2015-08-16 10:30:37
【问题描述】:

如何使用 python for linux 和 windows 在后台检测按键组合?

例如,

当检测到Ctrl+v 时,在后台执行doThis()

当检测到Tab 时,在后台执行doThat()

【问题讨论】:

    标签: linux windows python-3.x keypress


    【解决方案1】:

    如果您使用的是 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()
    

    【讨论】:

    • 感谢您的反馈。即使它被最小化,我怎样才能让它工作?
    • 您能否查看以下链接:如果它回答了您的问题stackoverflow.com/questions/515801/…
    • 只适用于 ubuntu、pyhook 和 pyxhook 似乎是合法的
    【解决方案2】:

    在 Windows 上,这可以使用 pyhook

    在 ubuntu 上,我在这个的帮助下做到了 pyxhook

    编辑:另一个很棒的 Windows 和 Linux 库 - keyboard

    【讨论】:

    【解决方案3】:

    pynput 最适合我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-11
      • 2023-03-03
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多