问题描述
我正在用 Python 编写一个小鼠标垃圾邮件阻止脚本,但我真的不知道我该怎么做 只能阻止鼠标点击,但仍然可以移动鼠标。
这是我目前得到的:
from pynput.mouse import Listener
import time
mouse_allowed = True
def timeout():
mouse_allowed = False
time.sleep(0.1)
def on_click(x,y,button,pressed):
if mouse_allowed:
print("Clicked")
timeout()
if mouse_allowed:
with Listener(on_click=on_click) as listener:
listener.join()
解决方法
我发现绕过鼠标锁定的一种方法是检查时间。这可以通过使用 time.time() 函数来完成。这是我的意思的粗略示例片段。
from pynput.mouse import Listener
import time
last_click = time.time()
def good_click_time():
# Make sure the program knows that it's getting the global variable
global last_click
# Makes sure that 1 second has gone by since the last click
if time.time() - last_click > 1:
# Records last click
last_click = time.time()
return True
else:
return False
def on_click(x,y,button,pressed):
if good_click_time():
print("Clicked")
else:
print("You must wait 1 second to click. You have waited {} seconds".format(time.time() - last_click))
with Listener(on_click=on_click) as listener:
listener.join()
通过记录您上次点击的确切时间,您可以使用它来检查自上次点击以来是否至少过了一秒(或您想要的点击间隔时间)。