Hi I wanted to bind extra buttons on my mouse using pynput for playing a game
Button.x1 for spam spacebar key
Button.x2 for spam left mouse
and all of them is trigger
for x1 button is worked. but the x2 is more likely holding the left mouse button, when I activate it
I've tried run the script on terminal (as admin). but still won't work
here is my code
from pynput.mouse import Listener, Button, Controller as MController
from pynput.keyboard import Key, Controller as KController
from time import sleep
from threading import Thread
from random import uniform
C_keyboard = KController()
C_mouse = MController()
state = [False, False]
def bind(i, key, c):
global state
while state[i]:
c.press(key)
sleep(round(uniform(0.5, 1.0), 10))
c.release(key)
def onClick(x, y, button, pressed):
global state
global C_keyboard
global C_mouse
if pressed:
if button == Button.x1:
state[0] = not state[0]
Thread(target=bind, args=(0, Key.space, C_keyboard)).start()
elif button == Button.x2:
state[1] = not state[1]
Thread(target=bind, args=(1, Button.left, C_mouse)).start()
print("%s is %s (%s, %s)" % (button, ("Pressed" if pressed else "Released"), state[0],state[1]))
def main():
with Listener(on_click=onClick) as listener:
listener.join()
Thread(target=main).start()
here is the output of my onClick function when I pressed x2 once
