我需要一个任务栏应用程序,不仅要响应鼠标单击(这不是问题),还要响应特定的键盘键组合(热键)。
我对Python还是很陌生,所以我尝试了一个基于wx.adv的示例,并尝试在其中合并pynput的侦听器。该代码不起作用,并且我收到未定义on_press的错误。
KeyComb_My = [
{pynput.keyboard.Key.ctrl, pynput.keyboard.KeyCode(char='s')},
{pynput.keyboard.Key.ctrl_l, pynput.keyboard.KeyCode(char='s')},
{pynput.keyboard.Key.ctrl_r, pynput.keyboard.KeyCode(char='s')}
]
class TaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
super(TaskBarIcon, self).__init__()
self.set_icon(TRAY_ICON)
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
self.keyb_listener()
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Site', self.on_click)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
return menu
def set_icon(self, path):
icon = wx.Icon(path)
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print ('Tray icon was left-clicked.')
def on_click(self, event):
frame = MyForm()
frame.Show()
def on_exit(self, event):
wx.CallAfter(self.Destroy)
self.frame.Close()
def on_press(self, key):
print(key)
if any([key in comb for comb in KeyComb_My]):
current.add(key)
if any(all(k in current for k in comb) for comb in
KeyComb_My):
print("Start")
frame = MyForm()
frame.Show()
def on_release(self, key):
try:
current.remove(key)
except KeyError:
pass
def keyb_listener(self):
current = set()
listener = pynput.keyboard.Listener(on_press=on_press,
on_release=on_release)
listener.start()
我希望定义的组合键将在on_press中启动并打开另一个窗口(MyForm)。但是实际输出是错误的。