Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # full code in answer to:
- # https://stackoverflow.com/questions/65889957/detecting-both-mouse-buttons-held-down/65890425#65890425
- from pynput import mouse
- global leftPressed, rightPressed, bothPressed
- leftPressed = False
- rightPressed = False
- bothPressed = False
- # This function will be called when any key of mouse is pressed
- def on_click(*args):
- global leftPressed, rightPressed, bothPressed
- #print(args)
- if args[-1]:
- # mouse key pressed
- print('The "{}" mouse key has held down'.format(args[-2].name))
- if args[-2].name == "left":
- leftPressed = True
- elif args[-2].name == "right":
- rightPressed = True
- if leftPressed and rightPressed:
- # if both left and right are pressed
- bothPressed = True
- print(bothPressed)
- elif not args[-1]:
- # mouse key released
- print('The "{}" mouse key is released'.format(args[-2].name))
- if args[-2].name == "left":
- leftPressed = False
- elif args[-2].name == "right":
- rightPressed = False
- # as one key has been released, both are no longer pressed
- bothPressed = False
- print(bothPressed)
- # Open Listener for mouse key presses
- with mouse.Listener(on_click=on_click) as listener:
- # Listen to the mouse key presses
- listener.join()
Add Comment
Please, Sign In to add comment