2

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?

Kamepoh2
  • 21
  • 1
  • 2

3 Answers3

4

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()
Laurenz Z
  • 81
  • 7
1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • If they are not keyboard events, is there a way in pynput to at least receive the events for those buttons? – The Elemental of Destruction Jan 06 '20 at 05:07
  • @TheElementalofCreation I don’t know. Have you tried registering listeners for keyboard and mouse events? – Martijn Pieters Jan 06 '20 at 08:12
  • @TheElementalofCreation (I see I missed out a link I had intended to be there; it’s added now). – Martijn Pieters Jan 06 '20 at 08:12
  • Neither of them picked anything up. However, while testing out other stuff, I found that if I add an event filter to the mouse listener then the mouse listener does SEE the events for them, but they never get passed to the other functions. This is problematic though because trying to access the data from there will often just completely crash my python interpreter with no warning. – The Elemental of Destruction Jan 06 '20 at 08:14
  • Specifically, in my case I was using the keyword argument "win32_event_filter" to pass a function that would save the two arguments of the function (msg and data) to an array that was global so I could mess with it freely. – The Elemental of Destruction Jan 06 '20 at 09:19
1

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()
Arash.g
  • 47
  • 1
  • 8