Skip to content

Commit

Permalink
Add injected argument to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
moses-palmer committed Mar 3, 2025
1 parent 2aeae03 commit 609dfe4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
16 changes: 10 additions & 6 deletions lib/pynput/keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,26 @@ def __init__(self, hotkeys, *args, **kwargs):
*args,
**kwargs)

def _on_press(self, key):
def _on_press(self, key, injected):
"""The press callback.
This is automatically registered upon creation.
:param key: The key provided by the base class.
:param injected: Whether the event was injected.
"""
for hotkey in self._hotkeys:
hotkey.press(self.canonical(key))
if not injected:
for hotkey in self._hotkeys:
hotkey.press(self.canonical(key))

def _on_release(self, key):
def _on_release(self, key, injected):
"""The release callback.
This is automatically registered upon creation.
:param key: The key provided by the base class.
:param injected: Whether the event was injected.
"""
for hotkey in self._hotkeys:
hotkey.release(self.canonical(key))
if not injected:
for hotkey in self._hotkeys:
hotkey.release(self.canonical(key))
22 changes: 16 additions & 6 deletions lib/pynput/keyboard/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,13 +657,23 @@ class Listener(AbstractListener):
:param callable on_press: The callback to call when a button is pressed.
It will be called with the argument ``(key)``, where ``key`` is a
:class:`KeyCode`, a :class:`Key` or ``None`` if the key is unknown.
It will be called with the arguments ``(key, injected)``, where ``key``
is a :class:`KeyCode`, a :class:`Key` or ``None`` if the key is
unknown, and ``injected`` whether the event was injected and thus not
generated by an actual input device.
Please note that not all backends support ``injected`` and will always
set it to ``False``.
:param callable on_release: The callback to call when a button is released.
It will be called with the argument ``(key)``, where ``key`` is a
:class:`KeyCode`, a :class:`Key` or ``None`` if the key is unknown.
It will be called with the arguments ``(key, injected)``, where ``key``
is a :class:`KeyCode`, a :class:`Key` or ``None`` if the key is
unknown, and ``injected`` whether the event was injected and thus not
generated by an actual input device.
Please note that not all backends support ``injected`` and will always
set it to ``False``.
:param bool suppress: Whether to suppress events. Setting this to ``True``
will prevent the input events from being passed to the rest of the
Expand Down Expand Up @@ -712,8 +722,8 @@ def __init__(self, on_press=None, on_release=None, suppress=False,
for key, value in kwargs.items()
if key.startswith(option_prefix)}
super(Listener, self).__init__(
on_press=self._wrap(on_press, 1),
on_release=self._wrap(on_release, 1),
on_press=self._wrap(on_press, 2),
on_release=self._wrap(on_release, 2),
suppress=suppress)
# pylint: enable=W0223

Expand Down
39 changes: 26 additions & 13 deletions lib/pynput/mouse/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,44 @@ class Listener(AbstractListener):
:param callable on_move: The callback to call when mouse move events occur.
It will be called with the arguments ``(x, y)``, which is the new
pointer position. If this callback raises :class:`StopException` or
returns ``False``, the listener is stopped.
It will be called with the arguments ``(x, y, injected)``, where ``(x,
y)`` is the new pointer position and ``injected`` whether the event was
injected and thus not generated by an actual input device. If this
callback raises :class:`StopException` or returns ``False``, the
listener is stopped.
Please note that not all backends support ``injected`` and will always
set it to ``False``.
:param callable on_click: The callback to call when a mouse button is
clicked.
It will be called with the arguments ``(x, y, button, pressed)``,
where ``(x, y)`` is the new pointer position, ``button`` is one of the
:class:`Button` values and ``pressed`` is whether the button was
pressed.
It will be called with the arguments ``(x, y, button, pressed,
injected)``, where ``(x, y)`` is the new pointer position, ``button``
is one of the :class:`Button`, ``pressed`` is whether the button was
pressed and ``injected`` whether the event was injected and thus not
generated by an actual input device.
If this callback raises :class:`StopException` or returns ``False``,
the listener is stopped.
Please note that not all backends support ``injected`` and will always
set it to ``False``.
:param callable on_scroll: The callback to call when mouse scroll
events occur.
It will be called with the arguments ``(x, y, dx, dy)``, where
``(x, y)`` is the new pointer position, and ``(dx, dy)`` is the scroll
vector.
It will be called with the arguments ``(x, y, dx, dy, injected)``,
where ``(x, y)`` is the new pointer position, and ``(dx, dy)`` is the
scroll vector and ``injected`` whether the event was injected and thus
not generated by an actual input device.
If this callback raises :class:`StopException` or returns ``False``,
the listener is stopped.
Please note that not all backends support ``injected`` and will always
set it to ``False``.
:param bool suppress: Whether to suppress events. Setting this to ``True``
will prevent the input events from being passed to the rest of the
system.
Expand Down Expand Up @@ -261,8 +274,8 @@ def __init__(self, on_move=None, on_click=None, on_scroll=None,
for key, value in kwargs.items()
if key.startswith(option_prefix)}
super(Listener, self).__init__(
on_move=self._wrap(on_move, 2),
on_click=self._wrap(on_click, 4),
on_scroll=self._wrap(on_scroll, 4),
on_move=self._wrap(on_move, 3),
on_click=self._wrap(on_click, 5),
on_scroll=self._wrap(on_scroll, 5),
suppress=suppress)
# pylint: enable=W0223

0 comments on commit 609dfe4

Please sign in to comment.