'Python 3.x - Multi mouse button activated auto clicker

I am having troubles with the python code i am trying to write. I am attempting to create an auto-clicker that when I hold down left click and one of the side buttons, left click gets triggered every 0.1 seconds. If I let go of either button, the auto-clicker stops. I have searched through the PyAutoGUI, PyNput, and the PyGame libraries, as well as many of stack overflow posts on the topic of mouse detection. Here is the code I have put together so far. (note, a majority of this code is not mine, I simply do not remember where I grabbed the code snippits otherwise they would be linked below.

from pynput import mouse
from pynput.mouse import Button, Controller
import threading
import time

control = Controller()

running = False
leftPressed=False
rightPressed=False

def process():
    print('start')
    count = 0
    while running:
        print(count)
        count += 1
        control.click(Button.left,1)
        print("test")
        time.sleep(.05)
    print('stop')
    
def on_click(*args):
    global running, leftPressed, rightPressed
    
    if args[-1]:
        # mouse key pressed
        
        
        if args[-2].name == "left":
            leftPressed = True
            print("Left click")
        elif args[-2].name == "x2":
            rightPressed = True
            
        if leftPressed and rightPressed:
            # if both left and right are pressed
            running = True
            threading.Thread(target=process).start()
                        
 
    elif not args[-1]:
        # mouse key released
        
        
        if args[-2].name == "x1":
            leftPressed = False
        elif args[-2].name == "x2":
            rightPressed = False
 
        # as one key has been released, both are no longer pressed
        running = False
        
        
with mouse.Listener(on_click=on_click) as listener:
    listener.join()

Any help with this project would be greatly appreciated, I've come to realize the biggest issue is that since the "on click" event is expecting a left click to run the thread, left clicking inside of the thread is more than not ideal, however if it is possible to program it in such a way where this isn't an issue it would be greatly appreciated.

The pseudocode for what I am looking for would be:

if Button.x2 == pressed and Button.left == pressed:
     bothPressed = True
else:
    bothPressed = False


while bothPressed:
     Button.left.click()

I have tried using multiple methods of obtaining the mouse inputs through PyGame and PyNput. However I've found that I understand PyNput the best and would prefer to stick with it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source