pygbutton/__init__.py
def handleEvent(self, eventObj):
        """All MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN event objects
        created by Pygame should be passed to this method. handleEvent() will
        detect if the event is relevant to this button and change its state.

        There are two ways that your code can respond to button-events. One is
        to inherit the PygButton class and override the mouse*() methods. The
        other is to have the caller of handleEvent() check the return value
        for the strings 'enter', 'move', 'down', 'up', 'click', or 'exit'.

        Note that mouseEnter() is always called before mouseMove(), and
        mouseMove() is always called before mouseExit(). Also, mouseUp() is
        always called before mouseClick().

        buttonDown is always True when mouseDown() is called, and always False
        when mouseUp() or mouseClick() is called. lastMouseDownOverButton is
        always False when mouseUp() or mouseClick() is called.

        NOTE: This help text needs to be modified for the different button
        clicks and hotkeys."""

        retVal = []

        # mouse-related events (and is visible)
        if eventObj.type in (MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN) and self._visible:

            # get mouse button clicked (-1 to match with indexing)
            if eventObj.type in (MOUSEBUTTONUP, MOUSEBUTTONDOWN):
                buttonIndex = eventObj.button - 1

            hasExited = False
            if not self.mouseOverButton and self._rect.collidepoint(eventObj.pos):
                # if mouse has entered the button:
                self.mouseOverButton = True
                self.mouseEnter(eventObj)
                retVal.append('enter')
            elif self.mouseOverButton and not self._rect.collidepoint(eventObj.pos):
                # if mouse has exited the button:
                self.mouseOverButton = False
                hasExited = True # call mouseExit() later, since we want mouseMove() to be handled before mouseExit()

            if self._rect.collidepoint(eventObj.pos):
                # if mouse event happened over the button:
                if eventObj.type == MOUSEMOTION:
                    self.mouseMove(eventObj)
                    retVal.append('move')
                elif eventObj.type == MOUSEBUTTONDOWN:
                    self.buttonDown[buttonIndex] = True
                    self.lastMouseDownOverButton[buttonIndex] = True
                    if buttonIndex == 0:
                        self.mouseLeftDown(eventObj)
                        retVal.append('left_down')
                    if buttonIndex == 1:
                        self.mouseMiddleDown(eventObj)
                        retVal.append('middle_down')
                    if buttonIndex == 2:
                        self.mouseRightDown(eventObj)
                        retVal.append('right_down')
            else:
                if eventObj.type in (MOUSEBUTTONUP, MOUSEBUTTONDOWN):
                    # if an up/down happens off the button, then the next up won't cause mouseClick()
                    self.lastMouseDownOverButton[buttonIndex] = False

            # mouse up is handled whether or not it was over the button
            doMouseClick = False
            if eventObj.type == MOUSEBUTTONUP:
                if self.lastMouseDownOverButton[buttonIndex]:
                    doMouseClick = True
                self.lastMouseDownOverButton[buttonIndex] = False

                if self.buttonDown[buttonIndex]:
                    self.buttonDown[buttonIndex] = False
                    if buttonIndex == 0:
                        self.mouseLeftUp(eventObj)
                        retVal.append('left_up')
                    if buttonIndex == 1:
                        self.mouseMiddleUp(eventObj)
                        retVal.append('middle_up')
                    if buttonIndex == 2:
                        self.mouseRightUp(eventObj)
                        retVal.append('right_up')

                if doMouseClick:
                    self.buttonDown[buttonIndex] = False
                    if buttonIndex == 0:
                        self.mouseLeftClick(eventObj)
                        retVal.append('left_click')
                    if buttonIndex == 1:
                        self.mouseMiddleClick(eventObj)
                        retVal.append('middle_click')
                    if buttonIndex == 2:
                        self.mouseRightClick(eventObj)
                        retVal.append('right_click')

            if hasExited:
                self.mouseExit(eventObj)
                retVal.append('exit')

            return retVal

        # hot-key related events (and is visible)
        elif eventObj.type in (KEYDOWN, KEYUP) and self._visible:
            # proceed only if a hotkey
            if eventObj.key in self.hotkeys:
                if eventObj.type == KEYDOWN:
                    self.hotkeyDown(eventObj)
                    retVal.append('hotkeydown')

                if eventObj.type == KEYUP:
                    self.hotkeyUp(eventObj)
                    retVal.append('hotkeyup')

            return retVal

        else:
            return retVal
All MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN event objects
        created by Pygame should be passed to this method. handleEvent() will
        detect if the event is relevant to this button and change its state.

        There are two ways that your code can respond to button-events. One is
        to inherit the PygButton class and override the mouse*() methods. The
        other is to have the caller of handleEvent() check the return value
        for the strings 'enter', 'move', 'down', 'up', 'click', or 'exit'.

        Note that mouseEnter() is always called before mouseMove(), and
        mouseMove() is always called before mouseExit(). Also, mouseUp() is
        always called before mouseClick().

        buttonDown is always True when mouseDown() is called, and always False
        when mouseUp() or mouseClick() is called. lastMouseDownOverButton is
        always False when mouseUp() or mouseClick() is called.

        NOTE: This help text needs to be modified for the different button
        clicks and hotkeys.
All MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN event objects
created by Pygame should be passed to this method. handleEvent() will
detect if the event is relevant to this button and change its state.

There are two ways that your code can respond to button-events. One is
to inherit the PygButton class and override the mouse*() methods.

Note that mouseEnter() is always called before mouseMove(), and
mouseMove() is always called before mouseExit(). Also, mouseUp() is
always called before mouseClick().

buttonDown is always True when mouseDown() is called, and always False
when mouseUp() or mouseClick() is called. lastMouseDownOverButton is
always False when mouseUp() or mouseClick() is called.

This help text needs to be modified for the different button
clicks and hotkeys.
All MOUSEMOTION MOUSEBUTTONUP MOUSEBUTTONDOWN event objects created by Pygame should be passed to this method . handleEvent () will detect if the event is relevant to this button and change its state . There are two ways that your code can respond to button - events . One is to inherit the PygButton class and override the mouse * () methods . Note that mouseEnter () is always called before mouseMove () and mouseMove () is always called before mouseExit () . Also mouseUp () is always called before mouseClick () . buttonDown is always True when mouseDown () is called and always False when mouseUp () or mouseClick () is called . lastMouseDownOverButton is always False when mouseUp () or mouseClick () is called . This help text needs to be modified for the different button clicks and hotkeys .
def handleEvent(self, eventObj):
        retVal = []
        if eventObj.type in (MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN) and self._visible:
            if eventObj.type in (MOUSEBUTTONUP, MOUSEBUTTONDOWN):
                buttonIndex = eventObj.button - 1
            hasExited = False
            if not self.mouseOverButton and self._rect.collidepoint(eventObj.pos):
                self.mouseOverButton = True
                self.mouseEnter(eventObj)
                retVal.append('enter')
            elif self.mouseOverButton and not self._rect.collidepoint(eventObj.pos):
                self.mouseOverButton = False
                hasExited = True 
            if self._rect.collidepoint(eventObj.pos):
                if eventObj.type == MOUSEMOTION:
                    self.mouseMove(eventObj)
                    retVal.append('move')
                elif eventObj.type == MOUSEBUTTONDOWN:
                    self.buttonDown[buttonIndex] = True
                    self.lastMouseDownOverButton[buttonIndex] = True
                    if buttonIndex == 0:
                        self.mouseLeftDown(eventObj)
                        retVal.append('left_down')
                    if buttonIndex == 1:
                        self.mouseMiddleDown(eventObj)
                        retVal.append('middle_down')
                    if buttonIndex == 2:
                        self.mouseRightDown(eventObj)
                        retVal.append('right_down')
            else:
                if eventObj.type in (MOUSEBUTTONUP, MOUSEBUTTONDOWN):
                    self.lastMouseDownOverButton[buttonIndex] = False
            doMouseClick = False
            if eventObj.type == MOUSEBUTTONUP:
                if self.lastMouseDownOverButton[buttonIndex]:
                    doMouseClick = True
                self.lastMouseDownOverButton[buttonIndex] = False
                if self.buttonDown[buttonIndex]:
                    self.buttonDown[buttonIndex] = False
                    if buttonIndex == 0:
                        self.mouseLeftUp(eventObj)
                        retVal.append('left_up')
                    if buttonIndex == 1:
                        self.mouseMiddleUp(eventObj)
                        retVal.append('middle_up')
                    if buttonIndex == 2:
                        self.mouseRightUp(eventObj)
                        retVal.append('right_up')
                if doMouseClick:
                    self.buttonDown[buttonIndex] = False
                    if buttonIndex == 0:
                        self.mouseLeftClick(eventObj)
                        retVal.append('left_click')
                    if buttonIndex == 1:
                        self.mouseMiddleClick(eventObj)
                        retVal.append('middle_click')
                    if buttonIndex == 2:
                        self.mouseRightClick(eventObj)
                        retVal.append('right_click')
            if hasExited:
                self.mouseExit(eventObj)
                retVal.append('exit')
            return retVal
        elif eventObj.type in (KEYDOWN, KEYUP) and self._visible:
            if eventObj.key in self.hotkeys:
                if eventObj.type == KEYDOWN:
                    self.hotkeyDown(eventObj)
                    retVal.append('hotkeydown')
                if eventObj.type == KEYUP:
                    self.hotkeyUp(eventObj)
                    retVal.append('hotkeyup')
            return retVal
        else:
            return retVal
def handleEvent ( self , eventObj ) : retVal = [ ] if eventObj . type in ( MOUSEMOTION , MOUSEBUTTONUP , MOUSEBUTTONDOWN ) and self . _visible : if eventObj . type in ( MOUSEBUTTONUP , MOUSEBUTTONDOWN ) : buttonIndex = eventObj . button - 1 hasExited = False if not self . mouseOverButton and self . _rect . collidepoint ( eventObj . pos ) : self . mouseOverButton = True self . mouseEnter ( eventObj ) retVal . append ( 'enter' ) elif self . mouseOverButton and not self . _rect . collidepoint ( eventObj . pos ) : self . mouseOverButton = False hasExited = True if self . _rect . collidepoint ( eventObj . pos ) : if eventObj . type == MOUSEMOTION : self . mouseMove ( eventObj ) retVal . append ( 'move' ) elif eventObj . type == MOUSEBUTTONDOWN : self . buttonDown [ buttonIndex ] = True self . lastMouseDownOverButton [ buttonIndex ] = True if buttonIndex == 0 : self . mouseLeftDown ( eventObj ) retVal . append ( 'left_down' ) if buttonIndex == 1 : self . mouseMiddleDown ( eventObj ) retVal . append ( 'middle_down' ) if buttonIndex == 2 : self . mouseRightDown ( eventObj ) retVal . append ( 'right_down' ) else : if eventObj . type in ( MOUSEBUTTONUP , MOUSEBUTTONDOWN ) : self . lastMouseDownOverButton [ buttonIndex ] = False doMouseClick = False if eventObj . type == MOUSEBUTTONUP : if self . lastMouseDownOverButton [ buttonIndex ] : doMouseClick = True self . lastMouseDownOverButton [ buttonIndex ] = False if self . buttonDown [ buttonIndex ] : self . buttonDown [ buttonIndex ] = False if buttonIndex == 0 : self . mouseLeftUp ( eventObj ) retVal . append ( 'left_up' ) if buttonIndex == 1 : self . mouseMiddleUp ( eventObj ) retVal . append ( 'middle_up' ) if buttonIndex == 2 : self . mouseRightUp ( eventObj ) retVal . append ( 'right_up' ) if doMouseClick : self . buttonDown [ buttonIndex ] = False if buttonIndex == 0 : self . mouseLeftClick ( eventObj ) retVal . append ( 'left_click' ) if buttonIndex == 1 : self . mouseMiddleClick ( eventObj ) retVal . append ( 'middle_click' ) if buttonIndex == 2 : self . mouseRightClick ( eventObj ) retVal . append ( 'right_click' ) if hasExited : self . mouseExit ( eventObj ) retVal . append ( 'exit' ) return retVal elif eventObj . type in ( KEYDOWN , KEYUP ) and self . _visible : if eventObj . key in self . hotkeys : if eventObj . type == KEYDOWN : self . hotkeyDown ( eventObj ) retVal . append ( 'hotkeydown' ) if eventObj . type == KEYUP : self . hotkeyUp ( eventObj ) retVal . append ( 'hotkeyup' ) return retVal else : return retVal
All MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN event objects
created by Pygame should be passed to this method.
All MOUSEMOTION MOUSEBUTTONUP MOUSEBUTTONDOWN event objects created by Pygame should be passed to this method .
"""All MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN event objects
        created by Pygame should be passed to this method. handleEvent() will
        detect if the event is relevant to this button and change its state.

        There are two ways that your code can respond to button-events. One is
        to inherit the PygButton class and override the mouse*() methods. The
        other is to have the caller of handleEvent() check the return value
        for the strings 'enter', 'move', 'down', 'up', 'click', or 'exit'.

        Note that mouseEnter() is always called before mouseMove(), and
        mouseMove() is always called before mouseExit(). Also, mouseUp() is
        always called before mouseClick().

        buttonDown is always True when mouseDown() is called, and always False
        when mouseUp() or mouseClick() is called. lastMouseDownOverButton is
        always False when mouseUp() or mouseClick() is called.

        NOTE: This help text needs to be modified for the different button
        clicks and hotkeys.""" # mouse-related events (and is visible) # get mouse button clicked (-1 to match with indexing) # if mouse has entered the button: # if mouse has exited the button: # call mouseExit() later, since we want mouseMove() to be handled before mouseExit() # if mouse event happened over the button: # if an up/down happens off the button, then the next up won't cause mouseClick() # mouse up is handled whether or not it was over the button # hot-key related events (and is visible) # proceed only if a hotkey
self
eventObj