mouse-repeat

It seems pretty trivial, but I can’t find the answer searching this forum…

Is there a repeat event for clicking the mouse button, like there is for the keyboard event, e.g. mouse1-repeat?

No. There’s no automatic mouse-button repeats provided by the OS. You just have to listen for mouse1 and mouse1-up, and assume the button is held down continuously between those events.

David

OK, thanks! I quickly wrote a small class that takes care of sending repeat events:

# Repeat events for holding down the mouse button(s) in Panda3d.

from direct.showbase.DirectObject import DirectObject
from direct.task import Task

class Repeater(DirectObject):
    """
    Repeat mouse button events while a button is held down.
    """

    def __init__(self, name, time):
        """
        Given a name of the event, e.g. mouse1, start
        sending name-repeat events every "time" seconds
        once event "name" is received until name-up is
        received.
        """
        self.time = time
        self.name = name
        self.task = None

        self.accept(name, self.start)
        self.accept(name + '-up', self.stop)

    def get_event(self):
        """
        Return the string that represents the event.
        """
        return '%s-repeat'%self.name
    
    def start(self):
        """
        Start sending repeat events.
        """
        if not self.task:
            self.task = taskMgr.doMethodLater(self.time, self.repeat,
                                              self.get_event())
    
    def stop(self):
        """
        Stop sending repeat events.
        """
        if self.task:
            taskMgr.remove(self.task)
            self.task = None
    
    def repeat(self, task):
        """
        Send repeat event.
        """
        messenger.send(self.get_event())
        return task.again

For example, to generate mouse1-repeat events every 0.5 seconds, create an instance like this:

repeater = Repeater('mouse1', 0.5)