[英]Python Key. keys
Hey I'm making an autoclicker with pyautogui and I don't know the keys I can put in for the Key.嘿,我正在用 pyautogui 制作一个自动点击器,但我不知道我可以为 Key 输入的密钥。 (variables at the beginning) I want to put in left mouse button down.
(变量开头)我想把鼠标左键按下。 If I just put a key it gives me an error so yeah I can't find this anywhere.
如果我只是放一个键,它会给我一个错误,所以是的,我在任何地方都找不到这个。
import pyautogui
from pynput.keyboard import *
from pynput import mouse
import random
# ======== settings ========
delay = random.uniform(.03,.22) # in seconds
resumeKey = Key.f4
pauseKey = Key.f6
exitKey = Key.esc
pause = True
running = True
def on_press(key):
global running, pause
if key == resumeKey:
pause = False
print("[Resumed]")
elif key == pauseKey:
pause = True
print("[Paused]")
elif key == exitKey:
running = False
print("[Exit]")
mouseLis = mouse.Listener(on_click = on_click)
mouseLis.start()
def on_click(x,y,button,pressed):
if button == mouse.Button.left:
pause = False
def main():
lis = Listener(on_press=on_press)
lis.start()
while running:
if not pause:
delay = random.uniform(.03,.22)
pyautogui.click(pyautogui.position())
pyautogui.PAUSE = delay
lis.stop()
mouseLis.stop()
if __name__ == "__main__":
main()
The mouse needs its own listener, it doesn't have a keybinding for the keyboard as far as I can tell.鼠标需要自己的监听器,据我所知,它没有键盘的键绑定。 Something like this: (Found in the documentation here .)
像这样:(在此处的文档中找到。)
from pynput import mouse
def on_click(x, y, button, pressed):
# do something here
mouse_lis = mouse.Listener(on_click=on_click)
mouse_lis.start()
To solve this I just created another function called on_click
that handled the mouse button presses.为了解决这个问题,我刚刚创建了另一个名为
on_click
的 function 来处理鼠标按钮按下。
This function was这个 function 是
def on_click(x,y,button,pressed):
if button == mouse.Button.left:
print("Output")
Basically it takes the click and if the button clicked was the left mouse button then it prints the output in the if statement.基本上它需要单击,如果单击的按钮是鼠标左键,那么它会在 if 语句中打印 output。 There are also three other parameters you have to mess around with if you would like.
如果您愿意,还必须处理其他三个参数。
In your main function add在你的主要 function 添加
mouseLis = mouse.Listener(on_click = on_click)
mouseLis.start()
This creates the actual mouse listener that will then work to listen to the mouse clicks.这将创建实际的鼠标侦听器,然后该侦听器将用于侦听鼠标点击。
At the end of your main function, be sure to include在你的主要 function 的末尾,一定要包括
mouseLis.stop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.