Reputation: 852
how can I detect key-press combinations in background using python for linux and windows ?
for example,
when
Ctrl+v
is detected executedoThis()
in backgroundwhen
Tab
is detected executedoThat()
in background
Upvotes: 2
Views: 3771
Reputation: 852
on windows this can be done using pyhook
on ubuntu I did it with help of this pyxhook
Edit: another awesome library for Windows & Linux - keyboard
Upvotes: 2
Reputation: 459
If you are using python tkinter, having filemenu. then below code might help you.
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()
Upvotes: 3