2

I am using Python Module "Mouse" to capture mouse inputs (more specifically, mouse coordinates, input button, and type of input). Source: https://github.com/boppreh/mouse

Looking at the source code for on_button() and on_click(), I am not sure how to pass the event_type back for "mouse button down" and "mouse button up" back to my code:

def left_mouse():
    print('LEFT ', mouse.get_position())
    # I want to get the [mouse button, example: left] and [type, example: up] from the logging

mouse.on_click(left_mouse)

Source code from MOUSE MODULE: on_button and on_click; I'm guessing I'm not calling callback appropriately but can't figure out what's the right approach:

def on_button(callback, args=(), buttons=(LEFT, MIDDLE, RIGHT, X, X2), types=(UP, DOWN, DOUBLE)):
    """ Invokes `callback` with `args` when the specified event happens. """
    if not isinstance(buttons, (tuple, list)):
        buttons = (buttons,)
    if not isinstance(types, (tuple, list)):
        types = (types,)

    def handler(event):
        if isinstance(event, ButtonEvent):
            if event.event_type in types and event.button in buttons:
                callback(*args)
    _listener.add_handler(handler)
    return handler

def on_click(callback, args=()):
    """ Invokes `callback` with `args` when the left button is clicked. """
    return on_button(callback, args, [LEFT], [UP])

Thanks as always.

2
  • The documentation is pretty specific on this function. It's always going to be the left mouse-button, and it's going to be triggered on a click (down). If you need the right mouse button to trigger, you use the on_right_click function. The function you're using is a synonym for on_left_click, they just shortened it to on_click :) docs Commented Nov 4, 2018 at 15:03
  • Thanks Torxed, I've got that to work. but that does not seem to allow me to retrieve the type of "up"/"down". Challenge I'm facing is not to get right/left button to trigger, but rather, once it is triggered, to get the parameters describing the action. Hope this clarifies. Commented Nov 5, 2018 at 13:34

1 Answer 1

2

So, according to the comments above you're struggling with retrieving if the event is "up" or "down".
Inherently, this information doesn't need to be delivered to you since this function you're using has one specific job. event_up (In my comments I said it was mouse down, but it's always up) and always "left mouse button". Meaning, there's no need to deliver this information to you - since the library function you're using, on_click() is a one way street without any variable outcomes.

You can try this, by executing your code and holding down the left mouse button, and nothing will happen until you release the button. That's when left+up triggers - the only outcome that can come from on_click().

The problem you're struggling with is probably that you're trying to attach your function left_mouse() to handle all mouse events?

Normally, you would attach one unique function per event - much like you have in your code with left_mouse(). But if you need to consolidate them for some reason, this is the way to go:

def mouse(event):
    print('Position:', mouse.get_position())
    print('Event:', event)

mouse.on_click(left_mouse, args=('mouse_left', 'mouse_up'))
mouse.on_righ_click(left_mouse, args=('mouse_right', 'mouse_up'))

But again, this is if you want to use one function in junction with the pre-defined library functions for the different mouse actions.

What you might be interested in, is the class mouse.ButtonEvent. This event has triggers and information attached to it. But to get this event/object you need to hook in and let the library know that you want to be in charge of each event. Meaning you can't use the pre-built functions for left-mouse-release etc, you need to built your own event handler. You do this by calling mouse.hook and then a function that you want to call upon each mouse event.

For instance:

import mouse
import time

events = []
mouse.hook(events.append)
while 1:
    mouse._listener.queue.join()
    for event in events:
        print(event)
    del events[:]
    time.sleep(0.25)

You register the function events.append (the regular list-append function) to be called on every mouse movement. (you could replace this with your own function as well, and handle the event straight in the function, much like you're trying to do).

From there, you can access each "frame" or mouse event and do things accordingly.

All this could be found in the libraries own test cases on how they test each function.
All be it not the first thing you look for, and it's pretty undocumented in terms of "how to get started".

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, the code is actually that simple. I guess I don't understand the callback concept as well as I should. Thank you very much for the wholesome answer, super appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.