python - 我如何在 python 中使用鼠标按钮 4-5(玩家)?

标签 python mouse pyautogui

我尝试在程序中使用鼠标按钮 4 和 5,但我只能使用 pyautogui 的右、左和中按钮,我需要另一个库吗?或者是什么?有人可以帮助我吗?

while True:
    data=conexion.read(size=1)
    if data ==bytes('7','utf-8'):
            pyautogui.hotkey('ctrl', 'c')
    elif data == bytes('4','utf-8'):
        pyautogui.hotkey('ctrl', 'v')
    elif data == bytes('8','utf-8'):
        pyautogui.hotkey('ctrl', 'f')
    elif data == bytes('C', 'utf-8'):
        pyautogui.hotkey('ctrl', 'u')
    elif data == bytes('*', 'utf-8'):
        buscar_cs()
    elif data == bytes('A', 'utf-8'):
        for i in range(5):
            pyautogui.moveTo(777,363)
            pyautogui.click(x=777,y=363,button='middle')
            pyautogui.scroll(-244)
            time.sleep(0.50)
    elif data == bytes('2', 'utf-8'):
        for i in range(5):
            pyautogui.moveTo(1022,284)
            pyautogui.click(x=1022,y=284,button='left')
            pyautogui.scroll(-244)
            time.sleep(0.25)
    elif data == bytes('D','utf-8'):
        pyautogui.alert(text="Cerrado", title='ALERTA', button='OK')
        break

非常感谢

最佳答案

我能做到!但我们需要另一个名为“pynput”的库。

Button.x1Button.x2 是鼠标按钮 4 和 5

按键代码:

import time
from pynput.mouse import Button, Controller

#press boton
mouse=Controller()
mouse.click(Button.x1)
time.sleep(1)
mouse.click(Button.x2)
time.sleep(1)

监听代码:

import time
from pynput import mouse

def on_click(x, y, button, pressed):
    if pressed == True and str(button)=="Button.x1":
        print("pressed x1")
    return

listener = mouse.Listener(
    on_click=on_click)
listener.start()

while True:
    time.sleep(0.2)

祝你好运!

关于python - 我如何在 python 中使用鼠标按钮 4-5(玩家)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73297856/

相关文章:

python - 签署 SimpleDB 请求

python - 无法通过 pip install 安装 pyautogui,出现错误 1

python - 为什么函数调用完成后变量仍然存在-python

javascript - 将数据结构从 Python 移植到 JS

python - 弃用警告 : using `oa_ndim == 0` when `op_axes` is NULL is deprecated

c# - 如何在不使用鼠标的情况下执行虚拟鼠标单击 C#

c++ - Winapi检测按钮悬停

c++ - QGraphicsItem 鼠标中键按下事件

python - Pyautogui 打字机只写数字

python-3.x - keyDown 功能不适用于 shift 键