我目前正在尝试编写一个简短的脚本,该脚本会在用户观看且不会干预时进行人力滚动(打开youtube链接)。我设法打开一个字母一个字母地缓慢插入链接,现在试图阻止用户输入。我尝试使用ctypes
导入来阻止所有输入,运行脚本,然后再次取消阻止,但是它不会以某种方式阻止输入。我刚刚收到我的RuntimeError消息。我该如何解决,使输入被阻止?提前致谢!这是代码:
import subprocess
import pyautogui
import time
import ctypes
from ctypes import wintypes
BlockInput = ctypes.windll.user32.BlockInput
BlockInput.argtypes = [wintypes.BOOL]
BlockInput.restype = wintypes.BOOL
blocked = BlockInput(True)
if blocked:
try:
subprocess.Popen(["C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",])
time.sleep(3)
pyautogui.write('www.youtube.com/watch?v=DLzxrzFCyOs', interval= 0.5)
pyautogui.hotkey('enter')
finally:
unblocked = BlockInput(False)
else:
raise RuntimeError('Input is already blocked by another thread')
您可以使用键盘模块阻止所有键盘输入,并使用鼠标模块不断移动鼠标,以防止用户移动鼠标。
有关更多详细信息,请参见以下链接:
https://github.com/boppreh/keyboard
https://github.com/boppreh/mouse
这会阻塞键盘上的所有键(150足够大以确保所有键都被阻塞)。
#### Blocking Keyboard ####
import keyboard
#blocks all keys of keyboard
for i in range(150):
keyboard.block_key(i)
通过不断将鼠标移动到位置(1,0),可以有效地阻止鼠标移动。
#### Blocking Mouse-movement ####
import threading
import mouse
import time
global executing
executing = True
def move_mouse():
#until executing is False, move mouse to (1,0)
global executing
while executing:
mouse.move(1,0, absolute=True, duration=0)
def stop_infinite_mouse_control():
#stops infinite control of mouse after 10 seconds if program fails to execute
global executing
time.sleep(10)
executing = False
threading.Thread(target=move_mouse).start()
threading.Thread(target=stop_infinite_mouse_control).start()
#^failsafe^
然后是您的原始代码(不再需要if语句和try / catch块)。
#### opening the video ####
import subprocess
import pyautogui
import time
subprocess.Popen(["C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",])
time.sleep(3)
pyautogui.write('www.youtube.com/watch?v=DLzxrzFCyOs', interval = 0.5)
pyautogui.hotkey('enter')
#### stops moving mouse to (1,0) after video has been opened
executing = False
只是一些注意事项:
在此处查看完整版本的代码:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句