在構建可以切換為垃圾郵件的腳本時。我遇到了以下問題。首先是腳本的正常運行版本:
import keyboard
import threading
def spam_this():
status = 0
while True:
if keyboard.is_pressed("F9") and status == 0:
status = 1
event.wait(1)
if keyboard.is_pressed("F9") and status == 1:
status = 0
event.wait(1)
while status == 1:
if keyboard.is_pressed("F9") and status == 1:
status = 0
event.wait(1)
print("test")
event = threading.Event()
threading.Thread(target=spam_this).start()
上面的腳本完美運行。但是,當我將行更改print("test")為keyboard.write("test"). 腳本中斷。
import keyboard
import threading
def spam_this():
status = 0
while True:
if keyboard.is_pressed("F9") and status == 0:
status = 1
event.wait(1)
if keyboard.is_pressed("F9") and status == 1:
status = 0
event.wait(1)
while status == 1:
if keyboard.is_pressed("F9") and status == 1:
status = 0
event.wait(1)
keyboard.write("test")
event = threading.Event()
threading.Thread(target=spam_this).start()
此版本的帶有該keyboard.write()功能的腳本可以使用隱含的切換鍵“F9”啟動,但是當我嘗試通過再次按下“F9”來關閉開關時,它不會像print("test")它自己的版本那樣停止。
注意:我不確定如何在標題中描述這個問題。我使用術語“阻塞”是因為其效果類似于time.sleep()嘗試創建while True:帶有切換的回圈時的阻塞方法。
uj5u.com熱心網友回復:
import threading
import keyboard
# global variable
status = False
def spam_this():
print('start: spam_this')
while True:
if status is True:
print("test")
keyboard.write("test")
event.wait(0.1)
def test(event=None):
global status
status = not status
print('change:', status)
event = threading.Event()
keyboard.add_hotkey("F9", test)
#alternatively:
#keyboard.on_key_press("F9", test)
threading.Thread(target=spam_this).start()
對@furas 的回答進行了一些修改。
對于該
test函式,event=None添加了一個引數,否則keyboard.on_press_key將F9按鍵按下事件傳遞給測驗函式,這將導致typeError.在
test函式中,event.wait()由于錯誤而被洗掉,經過測驗,keyboard.on_press_keyandkeyboard.add_hotkey函式確實有內置延遲,所以只要F9key沒有被按住,內置延遲就足夠了。無論是否使用
on_key_press或add_hotkey,都應在呼叫所需的“熱鍵方法”后初始化執行緒。否則執行緒會阻塞python腳本的主回圈。(這部分我無法解釋原因,只是反復試驗導致了這個結論。)將
keyboard.wait()被洗掉,因為沒有使用此功能。
數字 1 和 3 是最重要的變化(對于不想通讀次要部分的人)
uj5u.com熱心網友回復:
keyboard也可能運行自己的代碼,thread并且兩個執行緒之間可能會發生沖突。PythonGIL一次只運行一個執行緒 - 因此當您的執行緒運行時,它可能會阻塞應該在螢屏上寫入文本的執行緒。如果我even.wait(0.1)在之后使用代碼效果會更好,write()因此 Python 可以切換執行緒并在螢屏上發送文本。如果您使用更長的值,那么它也可以作業,但是如果您非常快地按鍵,那么它可能會繼續運行,write或者wait它無法檢查is_pressed("F9")也不會停止它 - 如果您event.wait(1)在之后使用,您應該看到它write
還有其他問題 - 它可能會在按鍵后寫入最后一個文本,它應該用于break退出while和跳過write
import threading
import keyboard
def spam_this():
status = 0
while True:
if keyboard.is_pressed("F9") and status == 0:
status = 1
event.wait(1)
if keyboard.is_pressed("F9") and status == 1:
status = 0
event.wait(1)
while status == 1:
if keyboard.is_pressed("F9") and status == 1:
print('stop')
status = 0
event.wait(1)
break
keyboard.write("test")
event.wait(0.1)
event = threading.Event()
threading.Thread(target=spam_this).start()
但我會把它減少到
import threading
import keyboard
def spam_this():
print('start spam_this')
status = False
while True:
if keyboard.is_pressed("F9"):
status = not status
print('status:', status)
event.wait(0.1)
if status is True:
keyboard.write("test")
event.wait(0.1)
event = threading.Event()
threading.Thread(target=spam_this).start()
但它還有另一個問題——如果你長時間按住按鍵,它會切換status很多次。我添加print('status:', status)以顯示它。
我想用keyboard.add_hotkey這樣的
import threading
import keyboard
# global variable
status = False
def spam_this():
print('start: spam_this')
while True:
if status is True:
keyboard.write("test")
event.wait(0.1)
def test():
global status
status = not status
print('change:', status)
event.wait(0.5)
event = threading.Event()
threading.Thread(target=spam_this).start()
keyboard.add_hotkey("F9", test)
keyboard.wait()
但是當您按住鍵時,系統可能會再次開始發送相同的鍵。
我沒有測驗keyboard.on_press_key()- 也許它會解決它。
檔案:鍵盤
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371853.html
