Repository: matplotlib/matplotlib
File: lib/matplotlib/backend_bases.py (Python)

Code before:
    """

    def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None):
        super().__init__(name, canvas, x, y, guiEvent=guiEvent)
        self.key = key

    @classmethod
    def _from_ax_coords(cls, name, ax, xy, key, *args, **kwargs):
        """Generate a synthetic event at a given axes coordinate."""
        # Separate from MouseEvent._from_ax_coords instead of being defined in the base
        # class, due to different parameter order in the constructor signature.
        x, y = ax.transData.transform(xy)
        event = cls(name, ax.figure.canvas, key, x, y, *args, **kwargs)
        event.inaxes = ax
        event.xdata, event.ydata = xy  # Force exact xy to avoid fp roundtrip issues.
        return event


# Default callback for key events.
def _key_handler(event):
    # Dead reckoning of key.
    if event.name == "key_press_event":
        event.canvas._key = event.key
    elif event.name == "key_release_event":
        event.canvas._key = None


# Default callback for mouse events.
def _mouse_handler(event):
    # Dead-reckoning of button and key.
    if event.name == "button_press_event":
        event.canvas._button = event.button
    elif event.name == "button_release_event":
        event.canvas._button = None
    elif event.name == "motion_notify_event" and event.button is None:
        event.button = event.canvas._button
    if event.key is None:
        event.key = event.canvas._key
    # Emit axes_enter/axes_leave.
    if event.name == "motion_notify_event":
        last_ref = LocationEvent._last_axes_ref
        last_axes = last_ref() if last_ref else None
        if last_axes != event.inaxes:
            if last_axes is not None:
                # Create a synthetic LocationEvent for the axes_leave_event.
                # Its inaxes attribute needs to be manually set (because the
                # cursor is actually *out* of that Axes at that point); this is
                # done with the internal _set_inaxes method which ensures that
                # the xdata and ydata attributes are also correct.
                try:
                    canvas = last_axes.get_figure(root=True).canvas

Review comment (suggestion):
Let's make the signature explicit. It's not worth employing `*args, **kwars` for a single parameter.

```suggestion
    def _from_ax_coords(cls, name, ax, xy, key, guiEvent=None):
        """
        Generate a synthetic event at a given axes coordinate.

        This is mainly intended for creating events during testing. The event can be send
        by calling its ``_process()`` method.
        """        
        # Separate from MouseEvent._from_ax_coords instead of being defined in the base
        # class, due to different parameter order in the constructor signature.
        x, y = ax.transData.transform(xy)
        event = cls(name, ax.figure.canvas, key, x, y, guiEvent)
```


Code after:
    Examples
    --------
    ::

        def on_key(event):
            print('you pressed', event.key, event.xdata, event.ydata)

        cid = fig.canvas.mpl_connect('key_press_event', on_key)
    """

    def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None):
        super().__init__(name, canvas, x, y, guiEvent=guiEvent)
        self.key = key

    @classmethod
    def _from_ax_coords(cls, name, ax, xy, key, *args, **kwargs):
        """
        Generate a synthetic event at a given axes coordinate.

        This method is intended for creating events during testing.  The event
        can be emitted by calling its ``_process()`` method.
        """
        # Separate from MouseEvent._from_ax_coords instead of being defined in the base
        # class, due to different parameter order in the constructor signature.
        x, y = ax.transData.transform(xy)
        event = cls(name, ax.figure.canvas, key, x, y, *args, **kwargs)
        event.inaxes = ax
        event.xdata, event.ydata = xy  # Force exact xy to avoid fp roundtrip issues.
        return event


# Default callback for key events.
def _key_handler(event):
    # Dead reckoning of key.
    if event.name == "key_press_event":
        event.canvas._key = event.key
    elif event.name == "key_release_event":
        event.canvas._key = None


# Default callback for mouse events.
def _mouse_handler(event):
    # Dead-reckoning of button and key.
    if event.name == "button_press_event":
        event.canvas._button = event.button
    elif event.name == "button_release_event":
        event.canvas._button = None
    elif event.name == "motion_notify_event" and event.button is None:
        event.button = event.canvas._button
    if event.key is None:
        event.key = event.canvas._key