我用tkinter
制作了一个GUI,您可以在其中指定两个热键。它具有活动和不活动按钮,它们从pynput
启动键盘监听器。当使用按钮将启动设置为活动状态时,我想用两个选择的键重新启动键盘监听器。
from tkinter import *
import threading
from pynput.keyboard import Key, Controller
from pynput import keyboard
state = False
key1 = "Shift"
key2 = "a"
def on_activate():
global state
if state == True:
#do stuff
#keyboard listener
def reading():
global key1
global key2
combination = "<"+key1+">+"+key2
print(combination)
with keyboard.GlobalHotKeys({
combination: on_activate}) as h:
print(keyboard.GlobalHotKeys)
h.join()
#dropdown change key1
def updateKey1(key):
global key1
key1 = key
print("updated key1: " + key1)
#dropdown change key2
def updateKey2(key):
global key2
key2 = key
print("updated key2: " + key2)
#button on
def turn_on():
global state
state = True
#button off
def turn_off():
global state
state = False
hotkey_thread = threading.Thread(target=reading, daemon=True)
if __name__ == '__main__':
hotkey_thread.start()
window.mainloop()```
因此,我设法使用listener.join()停止了侦听器,但是GUI没有响应。 这样是正确的尝试吗?有没有办法用新的键重新启动侦听器?