how i can use mouse button 4-5 (gamer) with python?

Viewed 71

I am trying to use mouse button 4 and 5 for a program but I can only use the right, left and middle button with pyautogui, do i need another library? or what? can somebody help me?

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

thanks a lot

2 Answers

I was able to do! but we need another library called "pynput".

Button.x1 and Button.x2 are mouse button 4 and 5

code for press boton:

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)

code for listening:

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)

Good Luck!

If you take a closer look at the __init__.py, from this module, you'll see a piece of code like this in the function click():

if platform.system() == "Linux":
    # Check for valid button arg on Linux:
    if button not in (LEFT, MIDDLE, RIGHT, PRIMARY, SECONDARY, 1, 2, 3, 4, 5, 6, 7):
        raise PyAutoGUIException(
            "button argument must be one of ('left', 'middle', 'right', 'primary', 'secondary', 1, 2, 3, 4, 5, 6, 7)"
        )
else:
    # Check for valid button arg on Windows and macOS:
    if button not in (LEFT, MIDDLE, RIGHT, PRIMARY, SECONDARY, 1, 2, 3):
        raise PyAutoGUIException(
            "button argument must be one of ('left', 'middle', 'right', 'primary', 'secondary', 1, 2, 3)"
        )

From here we can see that, for some reason, linux can receive the "extra buttons" that others OS can't.

By the way, you'll see an error a little behind that OS if/else check (mentioned above):

button = button.lower()

Here, if you try to pass any number as argument to the click() function you'll get that PyAutoGUIException, so I recommend you to change this line or make some verification like:

if type(button) == str:
    button = button.lower()
Related