使用 pynput 输入退出循环
Exit loop with pynput input
我的代码的目的是非常快速地连续键入一个字母。它在按下某个键(在本例中为 f3)时启动,在按下另一个键(f4)时停止。我的代码目前看起来像这样。
from pynput.keyboard import Controller, Listener
import time
keyboard = Controller()
confirm = False
def on_press(key):
if "f3" in str(key):
global confirm
confirm = True
while confirm:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.10)
elif "Key." in str(key):
pass
def exit_loop(key):
if "f4" in str(key):
global confirm
confirm = False
elif "Key." in str(key):
pass
with Listener(on_press=on_press) as ListenerStart:
ListenerStart.join()
with Listener(on_press=exit_loop) as ListenerEnd:
ListenerEnd.join()
我的问题是,在使用 f3 键启动程序时,我无法使用 f4 键停止程序。此外,该程序应该暂停,而不是退出。任何帮助,将不胜感激。谢谢
如果你有像 while
-loop 这样的长运行ning 代码,那么你必须 运行 它在单独的线程中 - 因为它会阻塞当前代码并且无法检查如果你按 f4
.
如果你想暂停代码,那么你应该使用一些变量 - 即。 paused = True
- 控制是否应执行或跳过 while
循环中的代码。
然后你只需要一个 Listener
来检查密钥并检查 paused
是 True
还是 False
from pynput.keyboard import Controller, Listener
import time
import threading
def function():
keyboard = Controller()
while True:
if not paused:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.1)
def on_press(key):
global paused
if paused:
if "f3" in str(key):
paused = False
else:
if "f4" in str(key):
paused = True
# global variables with default values at star
paused = True
# run long-running `function` in separated thread
thread = threading.Thread(target=function) # function's name without `()`
thread.start()
with Listener(on_press=on_press) as listener:
listener.join()
在正确的代码中,您可以使用相同的 f3
来启动和暂停循环。
from pynput.keyboard import Controller, Listener
import time
import threading
def function():
keyboard = Controller()
while True:
if not paused:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.1)
def on_press(key):
global paused
if "f3" in str(key):
paused = not paused
# global variables with default values at star
paused = True
# run long-running `function` in separated thread
thread = threading.Thread(target=function)
thread.start()
with Listener(on_press=on_press) as listener:
listener.join()
此代码可能更复杂 - f3
可以检查 thread
是否已经存在并在不存在时创建线程。
from pynput.keyboard import Controller, Listener
import time
import threading
def function():
keyboard = Controller()
while True:
if not paused:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.1)
def on_press(key):
global paused
global thread
if "f3" in str(key):
paused = not paused
if thread is None:
# run long-running `function` in separated thread
thread = threading.Thread(target=function)
thread.start()
# global variables with default values at star
paused = True
thread = None
with Listener(on_press=on_press) as listener:
listener.join()
我的代码的目的是非常快速地连续键入一个字母。它在按下某个键(在本例中为 f3)时启动,在按下另一个键(f4)时停止。我的代码目前看起来像这样。
from pynput.keyboard import Controller, Listener
import time
keyboard = Controller()
confirm = False
def on_press(key):
if "f3" in str(key):
global confirm
confirm = True
while confirm:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.10)
elif "Key." in str(key):
pass
def exit_loop(key):
if "f4" in str(key):
global confirm
confirm = False
elif "Key." in str(key):
pass
with Listener(on_press=on_press) as ListenerStart:
ListenerStart.join()
with Listener(on_press=exit_loop) as ListenerEnd:
ListenerEnd.join()
我的问题是,在使用 f3 键启动程序时,我无法使用 f4 键停止程序。此外,该程序应该暂停,而不是退出。任何帮助,将不胜感激。谢谢
如果你有像 while
-loop 这样的长运行ning 代码,那么你必须 运行 它在单独的线程中 - 因为它会阻塞当前代码并且无法检查如果你按 f4
.
如果你想暂停代码,那么你应该使用一些变量 - 即。 paused = True
- 控制是否应执行或跳过 while
循环中的代码。
然后你只需要一个 Listener
来检查密钥并检查 paused
是 True
还是 False
from pynput.keyboard import Controller, Listener
import time
import threading
def function():
keyboard = Controller()
while True:
if not paused:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.1)
def on_press(key):
global paused
if paused:
if "f3" in str(key):
paused = False
else:
if "f4" in str(key):
paused = True
# global variables with default values at star
paused = True
# run long-running `function` in separated thread
thread = threading.Thread(target=function) # function's name without `()`
thread.start()
with Listener(on_press=on_press) as listener:
listener.join()
在正确的代码中,您可以使用相同的 f3
来启动和暂停循环。
from pynput.keyboard import Controller, Listener
import time
import threading
def function():
keyboard = Controller()
while True:
if not paused:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.1)
def on_press(key):
global paused
if "f3" in str(key):
paused = not paused
# global variables with default values at star
paused = True
# run long-running `function` in separated thread
thread = threading.Thread(target=function)
thread.start()
with Listener(on_press=on_press) as listener:
listener.join()
此代码可能更复杂 - f3
可以检查 thread
是否已经存在并在不存在时创建线程。
from pynput.keyboard import Controller, Listener
import time
import threading
def function():
keyboard = Controller()
while True:
if not paused:
keyboard.press('e')
keyboard.release('e')
time.sleep(0.1)
def on_press(key):
global paused
global thread
if "f3" in str(key):
paused = not paused
if thread is None:
# run long-running `function` in separated thread
thread = threading.Thread(target=function)
thread.start()
# global variables with default values at star
paused = True
thread = None
with Listener(on_press=on_press) as listener:
listener.join()