Reputation: 41
I am using the Python module Pynput to make a macro that will press one of my side buttons. Does anyone know what the side buttons are called in Pynput? For example:
from pynput.mouse import Button, Controller
mouse = Controller()
mouse.press(Button.SIDEBUTTON)
mouse.release(Button.SIDEBUTTON)
What would go in the SIDEBUTTON part?
Upvotes: 4
Views: 9801
Reputation: 101
So this question is a bit old and i had the same problem. I figured out how these buttons are called:
its Button.x1 and Button.x2 for Mouse4 and Mouse5.
Hope I could help you. The script I used to find it is right here:
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
if pressed:
print(button)
# Collect events until released
with Listener(on_click=on_click) as listener:
listener.join()
Upvotes: 6
Reputation: 1123440
Additional buttons on mouse models are usually communicated as 'Button 6' and 'Button 7', etc. (buttons 4 and 5 are the scroll 'buttons'). Some mouse manufacturers send keyboard codes instead (like multimedia buttons or other custom codes).
For Windows and OS X Pynput only supports the left, right, middle mouse buttons however, so you'd be out of luck on those platforms as far as Pynput is concerned. If you are on Linux (with the X.org back-end), you can send and receive more buttons, from button8
all the way up to button31
, as well as scroll_up
, scroll_down
, scroll_left
and scroll_right
.
So depending on the mouse model you are using and your operating system, you may be able to get the right events for those buttons, be they mouse buttons or keyboard events. Register both a mouse and a keyboard listener, and print out the button value for on-click events for the mouse, and the key for keyboard press or release events, and see if you can get your side button to show up.
When not on Linux, if the specific mouse buttons are sent as a keyboard events, you are in luck and can use the keyboard controller to send the same events. If not, then Pynput can't send such mouse button events either.
That's not to say you can't send such button clicks at all, but you'd have to study the source code of the controller used for Windows or OSX and then see how the underlying framework would accept other button presses besides left, right and middle.
Upvotes: 1
Reputation: 47
unfortunately there isn't such a function in pynput
and if you run this code and press side mouse buttons you will realize that it wont give you any output:
from pynput import mouse
def on_click(x, y, button, pressed):
if pressed:
print ('Mouse clicked {2}'.format(x, y, button))
with mouse.Listener(on_click=on_click) as listener:
listener.join()
Upvotes: 1