first commit
This commit is contained in:
107
myenv/lib/python3.10/site-packages/pynput/mouse/__init__.py
Normal file
107
myenv/lib/python3.10/site-packages/pynput/mouse/__init__.py
Normal file
@@ -0,0 +1,107 @@
|
||||
# coding=utf-8
|
||||
# pynput
|
||||
# Copyright (C) 2015-2024 Moses Palmér
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under
|
||||
# the terms of the GNU Lesser General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
The module containing mouse classes.
|
||||
|
||||
See the documentation for more information.
|
||||
"""
|
||||
|
||||
# pylint: disable=C0103
|
||||
# Button, Controller and Listener are not constants
|
||||
|
||||
from pynput._util import backend, Events
|
||||
|
||||
|
||||
backend = backend(__name__)
|
||||
Button = backend.Button
|
||||
Controller = backend.Controller
|
||||
Listener = backend.Listener
|
||||
del backend
|
||||
|
||||
|
||||
class Events(Events):
|
||||
"""A mouse event listener supporting synchronous iteration over the events.
|
||||
|
||||
Possible events are:
|
||||
|
||||
:class:`Events.Move`
|
||||
The mouse was moved.
|
||||
|
||||
:class:`Events.Click`
|
||||
A mouse button was pressed or released.
|
||||
|
||||
:class:`Events.Scroll`
|
||||
The device was scrolled.
|
||||
"""
|
||||
_Listener = Listener
|
||||
|
||||
class Move(Events.Event):
|
||||
"""A move event.
|
||||
"""
|
||||
def __init__(self, x, y, injected):
|
||||
#: The X screen coordinate.
|
||||
self.x = x
|
||||
|
||||
#: The Y screen coordinate.
|
||||
self.y = y
|
||||
|
||||
#: Whether this event is synthetic.
|
||||
self.injected = injected
|
||||
|
||||
class Click(Events.Event):
|
||||
"""A click event.
|
||||
"""
|
||||
def __init__(self, x, y, button, pressed, injected):
|
||||
#: The X screen coordinate.
|
||||
self.x = x
|
||||
|
||||
#: The Y screen coordinate.
|
||||
self.y = y
|
||||
|
||||
#: The button.
|
||||
self.button = button
|
||||
|
||||
#: Whether the button was pressed.
|
||||
self.pressed = pressed
|
||||
|
||||
#: Whether this event is synthetic.
|
||||
self.injected = injected
|
||||
|
||||
class Scroll(Events.Event):
|
||||
"""A scroll event.
|
||||
"""
|
||||
def __init__(self, x, y, dx, dy, injected):
|
||||
#: The X screen coordinate.
|
||||
self.x = x
|
||||
|
||||
#: The Y screen coordinate.
|
||||
self.y = y
|
||||
|
||||
#: The number of horisontal steps.
|
||||
self.dx = dx
|
||||
|
||||
#: The number of vertical steps.
|
||||
self.dy = dy
|
||||
|
||||
#: Whether this event is synthetic.
|
||||
self.injected = injected
|
||||
|
||||
def __init__(self):
|
||||
super(Events, self).__init__(
|
||||
on_move=self.Move,
|
||||
on_click=self.Click,
|
||||
on_scroll=self.Scroll)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
281
myenv/lib/python3.10/site-packages/pynput/mouse/_base.py
Normal file
281
myenv/lib/python3.10/site-packages/pynput/mouse/_base.py
Normal file
@@ -0,0 +1,281 @@
|
||||
# coding=utf-8
|
||||
# pynput
|
||||
# Copyright (C) 2015-2024 Moses Palmér
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under
|
||||
# the terms of the GNU Lesser General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
This module contains the base implementation.
|
||||
|
||||
The actual interface to mouse classes is defined here, but the implementation
|
||||
is located in a platform dependent module.
|
||||
"""
|
||||
|
||||
# pylint: disable=R0903
|
||||
# We implement stubs
|
||||
|
||||
import enum
|
||||
|
||||
from pynput._util import AbstractListener, prefix
|
||||
from pynput import _logger
|
||||
|
||||
|
||||
class Button(enum.Enum):
|
||||
"""The various buttons.
|
||||
|
||||
The actual values for these items differ between platforms. Some
|
||||
platforms may have additional buttons, but these are guaranteed to be
|
||||
present everywhere.
|
||||
"""
|
||||
#: An unknown button was pressed
|
||||
unknown = 0
|
||||
|
||||
#: The left button
|
||||
left = 1
|
||||
|
||||
#: The middle button
|
||||
middle = 2
|
||||
|
||||
#: The right button
|
||||
right = 3
|
||||
|
||||
|
||||
class Controller(object):
|
||||
"""A controller for sending virtual mouse events to the system.
|
||||
"""
|
||||
def __init__(self):
|
||||
self._log = _logger(self.__class__)
|
||||
|
||||
@property
|
||||
def position(self):
|
||||
"""The current position of the mouse pointer.
|
||||
|
||||
This is the tuple ``(x, y)``, and setting it will move the pointer.
|
||||
"""
|
||||
return self._position_get()
|
||||
|
||||
@position.setter
|
||||
def position(self, pos):
|
||||
self._position_set(pos)
|
||||
|
||||
def scroll(self, dx, dy):
|
||||
"""Sends scroll events.
|
||||
|
||||
:param int dx: The horizontal scroll. The units of scrolling is
|
||||
undefined.
|
||||
|
||||
:param int dy: The vertical scroll. The units of scrolling is
|
||||
undefined.
|
||||
|
||||
:raises ValueError: if the values are invalid, for example out of
|
||||
bounds
|
||||
"""
|
||||
self._scroll(dx, dy)
|
||||
|
||||
def press(self, button):
|
||||
"""Emits a button press event at the current position.
|
||||
|
||||
:param Button button: The button to press.
|
||||
"""
|
||||
self._press(button)
|
||||
|
||||
def release(self, button):
|
||||
"""Emits a button release event at the current position.
|
||||
|
||||
:param Button button: The button to release.
|
||||
"""
|
||||
self._release(button)
|
||||
|
||||
def move(self, dx, dy):
|
||||
"""Moves the mouse pointer a number of pixels from its current
|
||||
position.
|
||||
|
||||
:param int dx: The horizontal offset.
|
||||
|
||||
:param int dy: The vertical offset.
|
||||
|
||||
:raises ValueError: if the values are invalid, for example out of
|
||||
bounds
|
||||
"""
|
||||
self.position = tuple(sum(i) for i in zip(self.position, (dx, dy)))
|
||||
|
||||
def click(self, button, count=1):
|
||||
"""Emits a button click event at the current position.
|
||||
|
||||
The default implementation sends a series of press and release events.
|
||||
|
||||
:param Button button: The button to click.
|
||||
|
||||
:param int count: The number of clicks to send.
|
||||
"""
|
||||
with self as controller:
|
||||
for _ in range(count):
|
||||
controller.press(button)
|
||||
controller.release(button)
|
||||
|
||||
def __enter__(self):
|
||||
"""Begins a series of clicks.
|
||||
|
||||
In the default :meth:`click` implementation, the return value of this
|
||||
method is used for the calls to :meth:`press` and :meth:`release`
|
||||
instead of ``self``.
|
||||
|
||||
The default implementation is a no-op.
|
||||
"""
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, value, traceback):
|
||||
"""Ends a series of clicks.
|
||||
"""
|
||||
pass
|
||||
|
||||
def _position_get(self):
|
||||
"""The implementation of the getter for :attr:`position`.
|
||||
|
||||
This is a platform dependent implementation.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def _position_set(self, pos):
|
||||
"""The implementation of the setter for :attr:`position`.
|
||||
|
||||
This is a platform dependent implementation.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def _scroll(self, dx, dy):
|
||||
"""The implementation of the :meth:`scroll` method.
|
||||
|
||||
This is a platform dependent implementation.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def _press(self, button):
|
||||
"""The implementation of the :meth:`press` method.
|
||||
|
||||
This is a platform dependent implementation.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def _release(self, button):
|
||||
"""The implementation of the :meth:`release` method.
|
||||
|
||||
This is a platform dependent implementation.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
# pylint: disable=W0223; This is also an abstract class
|
||||
class Listener(AbstractListener):
|
||||
"""A listener for mouse events.
|
||||
|
||||
Instances of this class can be used as context managers. This is equivalent
|
||||
to the following code::
|
||||
|
||||
listener.start()
|
||||
try:
|
||||
listener.wait()
|
||||
with_statements()
|
||||
finally:
|
||||
listener.stop()
|
||||
|
||||
This class inherits from :class:`threading.Thread` and supports all its
|
||||
methods. It will set :attr:`daemon` to ``True`` when created.
|
||||
|
||||
All callback arguments are optional; a callback may take less arguments
|
||||
than actually passed, but not more, unless they are optional.
|
||||
|
||||
:param callable on_move: The callback to call when mouse move events occur.
|
||||
|
||||
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,
|
||||
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, 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.
|
||||
|
||||
:param kwargs: Any non-standard platform dependent options. These should be
|
||||
prefixed with the platform name thus: ``darwin_``, ``xorg_`` or
|
||||
``win32_``.
|
||||
|
||||
Supported values are:
|
||||
|
||||
``darwin_intercept``
|
||||
A callable taking the arguments ``(event_type, event)``, where
|
||||
``event_type`` is any mouse related event type constant, and
|
||||
``event`` is a ``CGEventRef``.
|
||||
|
||||
This callable can freely modify the event using functions like
|
||||
``Quartz.CGEventSetIntegerValueField``. If this callable does not
|
||||
return the event, the event is suppressed system wide.
|
||||
|
||||
``win32_event_filter``
|
||||
A callable taking the arguments ``(msg, data)``, where ``msg`` is
|
||||
the current message, and ``data`` associated data as a
|
||||
`MSLLHOOKSTRUCT <https://docs.microsoft.com/en-gb/windows/win32/api/winuser/ns-winuser-msllhookstruct>`_.
|
||||
|
||||
If this callback returns ``False``, the event will not
|
||||
be propagated to the listener callback.
|
||||
|
||||
If ``self.suppress_event()`` is called, the event is suppressed
|
||||
system wide.
|
||||
"""
|
||||
def __init__(self, on_move=None, on_click=None, on_scroll=None,
|
||||
suppress=False, **kwargs):
|
||||
self._log = _logger(self.__class__)
|
||||
option_prefix = prefix(Listener, self.__class__)
|
||||
self._options = {
|
||||
key[len(option_prefix):]: value
|
||||
for key, value in kwargs.items()
|
||||
if key.startswith(option_prefix)}
|
||||
super(Listener, self).__init__(
|
||||
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
|
||||
212
myenv/lib/python3.10/site-packages/pynput/mouse/_darwin.py
Normal file
212
myenv/lib/python3.10/site-packages/pynput/mouse/_darwin.py
Normal file
@@ -0,0 +1,212 @@
|
||||
# coding=utf-8
|
||||
# pynput
|
||||
# Copyright (C) 2015-2024 Moses Palmér
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under
|
||||
# the terms of the GNU Lesser General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
The mouse implementation for *macOS*.
|
||||
"""
|
||||
|
||||
# pylint: disable=C0111
|
||||
# The documentation is extracted from the base classes
|
||||
|
||||
# pylint: disable=R0903
|
||||
# We implement stubs
|
||||
|
||||
import enum
|
||||
import Quartz
|
||||
|
||||
from AppKit import NSEvent
|
||||
|
||||
from pynput._util.darwin import (
|
||||
ListenerMixin)
|
||||
from . import _base
|
||||
|
||||
|
||||
def _button_value(base_name, mouse_button):
|
||||
"""Generates the value tuple for a :class:`Button` value.
|
||||
|
||||
:param str base_name: The base name for the button. This should be a string
|
||||
like ``'kCGEventLeftMouse'``.
|
||||
|
||||
:param int mouse_button: The mouse button ID.
|
||||
|
||||
:return: a value tuple
|
||||
"""
|
||||
return (
|
||||
tuple(
|
||||
getattr(Quartz, '%sMouse%s' % (base_name, name))
|
||||
for name in ('Down', 'Up', 'Dragged')),
|
||||
mouse_button)
|
||||
|
||||
|
||||
class Button(enum.Enum):
|
||||
"""The various buttons.
|
||||
"""
|
||||
unknown = None
|
||||
left = _button_value('kCGEventLeft', 0)
|
||||
middle = _button_value('kCGEventOther', 2)
|
||||
right = _button_value('kCGEventRight', 1)
|
||||
|
||||
|
||||
class Controller(_base.Controller):
|
||||
#: The scroll speed
|
||||
_SCROLL_SPEED = 10
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Controller, self).__init__(*args, **kwargs)
|
||||
self._click = None
|
||||
self._drag_button = None
|
||||
|
||||
def _position_get(self):
|
||||
pos = NSEvent.mouseLocation()
|
||||
|
||||
return pos.x, Quartz.CGDisplayPixelsHigh(0) - pos.y
|
||||
|
||||
def _position_set(self, pos):
|
||||
try:
|
||||
(_, _, mouse_type), mouse_button = self._drag_button.value
|
||||
except AttributeError:
|
||||
mouse_type = Quartz.kCGEventMouseMoved
|
||||
mouse_button = 0
|
||||
|
||||
Quartz.CGEventPost(
|
||||
Quartz.kCGHIDEventTap,
|
||||
Quartz.CGEventCreateMouseEvent(
|
||||
None,
|
||||
mouse_type,
|
||||
pos,
|
||||
mouse_button))
|
||||
|
||||
def _scroll(self, dx, dy):
|
||||
dx = int(dx)
|
||||
dy = int(dy)
|
||||
|
||||
Quartz.CGEventPost(
|
||||
Quartz.kCGHIDEventTap,
|
||||
Quartz.CGEventCreateScrollWheelEvent(
|
||||
None,
|
||||
Quartz.kCGScrollEventUnitPixel,
|
||||
2,
|
||||
dy * self._SCROLL_SPEED,
|
||||
dx * self._SCROLL_SPEED))
|
||||
|
||||
def _press(self, button):
|
||||
(press, _, _), mouse_button = button.value
|
||||
event = Quartz.CGEventCreateMouseEvent(
|
||||
None,
|
||||
press,
|
||||
self.position,
|
||||
mouse_button)
|
||||
|
||||
# If we are performing a click, we need to set this state flag
|
||||
if self._click is not None:
|
||||
self._click += 1
|
||||
Quartz.CGEventSetIntegerValueField(
|
||||
event,
|
||||
Quartz.kCGMouseEventClickState,
|
||||
self._click)
|
||||
|
||||
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)
|
||||
|
||||
# Store the button to enable dragging
|
||||
self._drag_button = button
|
||||
|
||||
def _release(self, button):
|
||||
(_, release, _), mouse_button = button.value
|
||||
event = Quartz.CGEventCreateMouseEvent(
|
||||
None,
|
||||
release,
|
||||
self.position,
|
||||
mouse_button)
|
||||
|
||||
# If we are performing a click, we need to set this state flag
|
||||
if self._click is not None:
|
||||
Quartz.CGEventSetIntegerValueField(
|
||||
event,
|
||||
Quartz.kCGMouseEventClickState,
|
||||
self._click)
|
||||
|
||||
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)
|
||||
|
||||
if button == self._drag_button:
|
||||
self._drag_button = None
|
||||
|
||||
def __enter__(self):
|
||||
self._click = 0
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, value, traceback):
|
||||
self._click = None
|
||||
|
||||
|
||||
class Listener(ListenerMixin, _base.Listener):
|
||||
#: The events that we listen to
|
||||
_EVENTS = (
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventMouseMoved) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventLeftMouseDown) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventLeftMouseUp) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventLeftMouseDragged) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventRightMouseDown) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventRightMouseUp) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventRightMouseDragged) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventOtherMouseDown) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventOtherMouseUp) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventOtherMouseDragged) |
|
||||
Quartz.CGEventMaskBit(Quartz.kCGEventScrollWheel))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Listener, self).__init__(*args, **kwargs)
|
||||
self._intercept = self._options.get(
|
||||
'intercept',
|
||||
None)
|
||||
|
||||
def _handle_message(self, _proxy, event_type, event, _refcon, injected):
|
||||
"""The callback registered with *macOS* for mouse events.
|
||||
|
||||
This method will call the callbacks registered on initialisation.
|
||||
"""
|
||||
try:
|
||||
(px, py) = Quartz.CGEventGetLocation(event)
|
||||
except AttributeError:
|
||||
# This happens during teardown of the virtual machine
|
||||
return
|
||||
|
||||
# Quickly detect the most common event type
|
||||
if event_type == Quartz.kCGEventMouseMoved:
|
||||
self.on_move(px, py, injected)
|
||||
|
||||
elif event_type == Quartz.kCGEventScrollWheel:
|
||||
dx = Quartz.CGEventGetIntegerValueField(
|
||||
event,
|
||||
Quartz.kCGScrollWheelEventDeltaAxis2)
|
||||
dy = Quartz.CGEventGetIntegerValueField(
|
||||
event,
|
||||
Quartz.kCGScrollWheelEventDeltaAxis1)
|
||||
self.on_scroll(px, py, dx, dy, injected)
|
||||
|
||||
else:
|
||||
for button in Button:
|
||||
try:
|
||||
(press, release, drag), _ = button.value
|
||||
except TypeError:
|
||||
# Button.unknown cannot be enumerated
|
||||
continue
|
||||
|
||||
# Press and release generate click events, and drag
|
||||
# generates move events
|
||||
if event_type in (press, release):
|
||||
self.on_click(px, py, button, event_type == press, injected)
|
||||
elif event_type == drag:
|
||||
self.on_move(px, py, injected)
|
||||
22
myenv/lib/python3.10/site-packages/pynput/mouse/_dummy.py
Normal file
22
myenv/lib/python3.10/site-packages/pynput/mouse/_dummy.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# pynput
|
||||
# Copyright (C) 2015-2024 Moses Palmér
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under
|
||||
# the terms of the GNU Lesser General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
This module contains a dummy implementation.
|
||||
|
||||
It cannot be used, but importing it will not raise any exceptions.
|
||||
"""
|
||||
|
||||
from ._base import Button, Controller, Listener
|
||||
226
myenv/lib/python3.10/site-packages/pynput/mouse/_win32.py
Normal file
226
myenv/lib/python3.10/site-packages/pynput/mouse/_win32.py
Normal file
@@ -0,0 +1,226 @@
|
||||
# coding=utf-8
|
||||
# pynput
|
||||
# Copyright (C) 2015-2024 Moses Palmér
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under
|
||||
# the terms of the GNU Lesser General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
The mouse implementation for *Windows*.
|
||||
"""
|
||||
|
||||
# pylint: disable=C0111
|
||||
# The documentation is extracted from the base classes
|
||||
|
||||
# pylint: disable=R0903
|
||||
# We implement stubs
|
||||
|
||||
import ctypes
|
||||
import enum
|
||||
|
||||
from ctypes import (
|
||||
windll,
|
||||
wintypes)
|
||||
|
||||
from pynput._util import NotifierMixin
|
||||
from pynput._util.win32 import (
|
||||
INPUT,
|
||||
INPUT_union,
|
||||
ListenerMixin,
|
||||
MOUSEINPUT,
|
||||
SendInput,
|
||||
SystemHook)
|
||||
from . import _base
|
||||
|
||||
#: A constant used as a factor when constructing mouse scroll data.
|
||||
WHEEL_DELTA = 120
|
||||
|
||||
|
||||
class Button(enum.Enum):
|
||||
"""The various buttons.
|
||||
"""
|
||||
unknown = None
|
||||
left = (MOUSEINPUT.LEFTUP, MOUSEINPUT.LEFTDOWN, 0)
|
||||
middle = (MOUSEINPUT.MIDDLEUP, MOUSEINPUT.MIDDLEDOWN, 0)
|
||||
right = (MOUSEINPUT.RIGHTUP, MOUSEINPUT.RIGHTDOWN, 0)
|
||||
x1 = (MOUSEINPUT.XUP, MOUSEINPUT.XDOWN, MOUSEINPUT.XBUTTON1)
|
||||
x2 = (MOUSEINPUT.XUP, MOUSEINPUT.XDOWN, MOUSEINPUT.XBUTTON2)
|
||||
|
||||
|
||||
class Controller(NotifierMixin, _base.Controller):
|
||||
__GetCursorPos = windll.user32.GetCursorPos
|
||||
__SetCursorPos = windll.user32.SetCursorPos
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Controller, self).__init__(*args, **kwargs)
|
||||
|
||||
def _position_get(self):
|
||||
point = wintypes.POINT()
|
||||
if self.__GetCursorPos(ctypes.byref(point)):
|
||||
return (point.x, point.y)
|
||||
else:
|
||||
return None
|
||||
|
||||
def _position_set(self, pos):
|
||||
pos = int(pos[0]), int(pos[1])
|
||||
self.__SetCursorPos(*pos)
|
||||
self._emit('on_move', *pos, True)
|
||||
|
||||
def _scroll(self, dx, dy):
|
||||
if dy:
|
||||
SendInput(
|
||||
1,
|
||||
ctypes.byref(INPUT(
|
||||
type=INPUT.MOUSE,
|
||||
value=INPUT_union(
|
||||
mi=MOUSEINPUT(
|
||||
dwFlags=MOUSEINPUT.WHEEL,
|
||||
mouseData=int(dy * WHEEL_DELTA))))),
|
||||
ctypes.sizeof(INPUT))
|
||||
|
||||
if dx:
|
||||
SendInput(
|
||||
1,
|
||||
ctypes.byref(INPUT(
|
||||
type=INPUT.MOUSE,
|
||||
value=INPUT_union(
|
||||
mi=MOUSEINPUT(
|
||||
dwFlags=MOUSEINPUT.HWHEEL,
|
||||
mouseData=int(dx * WHEEL_DELTA))))),
|
||||
ctypes.sizeof(INPUT))
|
||||
|
||||
if dx or dy:
|
||||
px, py = self._position_get()
|
||||
self._emit('on_scroll', px, py, dx, dy, True)
|
||||
|
||||
def _press(self, button):
|
||||
SendInput(
|
||||
1,
|
||||
ctypes.byref(INPUT(
|
||||
type=INPUT.MOUSE,
|
||||
value=INPUT_union(
|
||||
mi=MOUSEINPUT(
|
||||
dwFlags=button.value[1],
|
||||
mouseData=button.value[2])))),
|
||||
ctypes.sizeof(INPUT))
|
||||
|
||||
def _release(self, button):
|
||||
SendInput(
|
||||
1,
|
||||
ctypes.byref(INPUT(
|
||||
type=INPUT.MOUSE,
|
||||
value=INPUT_union(
|
||||
mi=MOUSEINPUT(
|
||||
dwFlags=button.value[0],
|
||||
mouseData=button.value[2])))),
|
||||
ctypes.sizeof(INPUT))
|
||||
|
||||
|
||||
@Controller._receiver
|
||||
class Listener(ListenerMixin, _base.Listener):
|
||||
#: The Windows hook ID for low level mouse events, ``WH_MOUSE_LL``
|
||||
_EVENTS = 14
|
||||
|
||||
WM_LBUTTONDOWN = 0x0201
|
||||
WM_LBUTTONUP = 0x0202
|
||||
WM_MBUTTONDOWN = 0x0207
|
||||
WM_MBUTTONUP = 0x0208
|
||||
WM_MOUSEMOVE = 0x0200
|
||||
WM_MOUSEWHEEL = 0x020A
|
||||
WM_MOUSEHWHEEL = 0x020E
|
||||
WM_RBUTTONDOWN = 0x0204
|
||||
WM_RBUTTONUP = 0x0205
|
||||
WM_XBUTTONDOWN = 0x20B
|
||||
WM_XBUTTONUP = 0x20C
|
||||
|
||||
MK_XBUTTON1 = 0x0020
|
||||
MK_XBUTTON2 = 0x0040
|
||||
|
||||
XBUTTON1 = 1
|
||||
XBUTTON2 = 2
|
||||
|
||||
#: A mapping from messages to button events
|
||||
CLICK_BUTTONS = {
|
||||
WM_LBUTTONDOWN: (Button.left, True),
|
||||
WM_LBUTTONUP: (Button.left, False),
|
||||
WM_MBUTTONDOWN: (Button.middle, True),
|
||||
WM_MBUTTONUP: (Button.middle, False),
|
||||
WM_RBUTTONDOWN: (Button.right, True),
|
||||
WM_RBUTTONUP: (Button.right, False)}
|
||||
|
||||
#: A mapping from message to X button events.
|
||||
X_BUTTONS = {
|
||||
WM_XBUTTONDOWN: {
|
||||
XBUTTON1: (Button.x1, True),
|
||||
XBUTTON2: (Button.x2, True)},
|
||||
WM_XBUTTONUP: {
|
||||
XBUTTON1: (Button.x1, False),
|
||||
XBUTTON2: (Button.x2, False)}}
|
||||
|
||||
#: A mapping from messages to scroll vectors
|
||||
SCROLL_BUTTONS = {
|
||||
WM_MOUSEWHEEL: (0, 1),
|
||||
WM_MOUSEHWHEEL: (1, 0)}
|
||||
|
||||
_HANDLED_EXCEPTIONS = (
|
||||
SystemHook.SuppressException,)
|
||||
|
||||
class _MSLLHOOKSTRUCT(ctypes.Structure):
|
||||
"""Contains information about a mouse event passed to a ``WH_MOUSE_LL``
|
||||
hook procedure, ``MouseProc``.
|
||||
"""
|
||||
LLMHF_INJECTED = 0x00000001
|
||||
LLMHF_LOWER_IL_INJECTED = 0x00000002
|
||||
_fields_ = [
|
||||
('pt', wintypes.POINT),
|
||||
('mouseData', wintypes.DWORD),
|
||||
('flags', wintypes.DWORD),
|
||||
('time', wintypes.DWORD),
|
||||
('dwExtraInfo', ctypes.c_void_p)]
|
||||
|
||||
#: A pointer to a :class:`_MSLLHOOKSTRUCT`
|
||||
_LPMSLLHOOKSTRUCT = ctypes.POINTER(_MSLLHOOKSTRUCT)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Listener, self).__init__(*args, **kwargs)
|
||||
self._event_filter = self._options.get(
|
||||
'event_filter',
|
||||
lambda msg, data: True)
|
||||
|
||||
def _handle_message(self, code, msg, lpdata):
|
||||
if code != SystemHook.HC_ACTION:
|
||||
return
|
||||
|
||||
data = ctypes.cast(lpdata, self._LPMSLLHOOKSTRUCT).contents
|
||||
injected = data.flags & (0
|
||||
| self._MSLLHOOKSTRUCT.LLMHF_INJECTED
|
||||
| self._MSLLHOOKSTRUCT.LLMHF_LOWER_IL_INJECTED) != 0
|
||||
|
||||
# Suppress further propagation of the event if it is filtered
|
||||
if self._event_filter(msg, data) is False:
|
||||
return
|
||||
|
||||
if msg == self.WM_MOUSEMOVE:
|
||||
self.on_move(data.pt.x, data.pt.y,injected)
|
||||
|
||||
elif msg in self.CLICK_BUTTONS:
|
||||
button, pressed = self.CLICK_BUTTONS[msg]
|
||||
self.on_click(data.pt.x, data.pt.y, button, pressed, injected)
|
||||
|
||||
elif msg in self.X_BUTTONS:
|
||||
button, pressed = self.X_BUTTONS[msg][data.mouseData >> 16]
|
||||
self.on_click(data.pt.x, data.pt.y, button, pressed, injected)
|
||||
|
||||
elif msg in self.SCROLL_BUTTONS:
|
||||
mx, my = self.SCROLL_BUTTONS[msg]
|
||||
dd = wintypes.SHORT(data.mouseData >> 16).value // WHEEL_DELTA
|
||||
self.on_scroll(data.pt.x, data.pt.y, dd * mx, dd * my, injected)
|
||||
184
myenv/lib/python3.10/site-packages/pynput/mouse/_xorg.py
Normal file
184
myenv/lib/python3.10/site-packages/pynput/mouse/_xorg.py
Normal file
@@ -0,0 +1,184 @@
|
||||
# coding=utf-8
|
||||
# pynput
|
||||
# Copyright (C) 2015-2024 Moses Palmér
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under
|
||||
# the terms of the GNU Lesser General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||
# details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
The keyboard implementation for *Xorg*.
|
||||
"""
|
||||
|
||||
# pylint: disable=C0111
|
||||
# The documentation is extracted from the base classes
|
||||
|
||||
|
||||
# pylint: disable=E1101,E1102
|
||||
# We dynamically generate the Button class
|
||||
|
||||
# pylint: disable=R0903
|
||||
# We implement stubs
|
||||
|
||||
# pylint: disable=W0611
|
||||
try:
|
||||
import pynput._util.xorg
|
||||
except Exception as e:
|
||||
raise ImportError('failed to acquire X connection: {}'.format(str(e)), e)
|
||||
# pylint: enable=W0611
|
||||
|
||||
import enum
|
||||
import Xlib.display
|
||||
import Xlib.ext
|
||||
import Xlib.ext.xtest
|
||||
import Xlib.X
|
||||
import Xlib.protocol
|
||||
|
||||
from pynput._util.xorg import (
|
||||
display_manager,
|
||||
ListenerMixin)
|
||||
from . import _base
|
||||
|
||||
|
||||
# pylint: disable=C0103
|
||||
Button = enum.Enum(
|
||||
'Button',
|
||||
module=__name__,
|
||||
names=[
|
||||
('unknown', None),
|
||||
('left', 1),
|
||||
('middle', 2),
|
||||
('right', 3),
|
||||
('scroll_up', 4),
|
||||
('scroll_down', 5),
|
||||
('scroll_left', 6),
|
||||
('scroll_right', 7)] + [
|
||||
('button%d' % i, i)
|
||||
for i in range(8, 31)])
|
||||
# pylint: enable=C0103
|
||||
|
||||
|
||||
class Controller(_base.Controller):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Controller, self).__init__(*args, **kwargs)
|
||||
self._display = Xlib.display.Display()
|
||||
|
||||
def __del__(self):
|
||||
if hasattr(self, '_display'):
|
||||
self._display.close()
|
||||
|
||||
def _position_get(self):
|
||||
with display_manager(self._display) as dm:
|
||||
qp = dm.screen().root.query_pointer()
|
||||
return (qp.root_x, qp.root_y)
|
||||
|
||||
def _position_set(self, pos):
|
||||
px, py = self._check_bounds(*pos)
|
||||
with display_manager(self._display) as dm:
|
||||
Xlib.ext.xtest.fake_input(dm, Xlib.X.MotionNotify, x=px, y=py)
|
||||
|
||||
def _scroll(self, dx, dy):
|
||||
dx, dy = self._check_bounds(dx, dy)
|
||||
if dy:
|
||||
self.click(
|
||||
button=Button.scroll_up if dy > 0 else Button.scroll_down,
|
||||
count=abs(dy))
|
||||
|
||||
if dx:
|
||||
self.click(
|
||||
button=Button.scroll_right if dx > 0 else Button.scroll_left,
|
||||
count=abs(dx))
|
||||
|
||||
def _press(self, button):
|
||||
with display_manager(self._display) as dm:
|
||||
Xlib.ext.xtest.fake_input(dm, Xlib.X.ButtonPress, button.value)
|
||||
|
||||
def _release(self, button):
|
||||
with display_manager(self._display) as dm:
|
||||
Xlib.ext.xtest.fake_input(dm, Xlib.X.ButtonRelease, button.value)
|
||||
|
||||
def _check_bounds(self, *args):
|
||||
"""Checks the arguments and makes sure they are within the bounds of a
|
||||
short integer.
|
||||
|
||||
:param args: The values to verify.
|
||||
"""
|
||||
if not all(
|
||||
(-0x7fff - 1) <= number <= 0x7fff
|
||||
for number in args):
|
||||
raise ValueError(args)
|
||||
else:
|
||||
return tuple(int(p) for p in args)
|
||||
|
||||
|
||||
class Listener(ListenerMixin, _base.Listener):
|
||||
#: A mapping from button values to scroll directions
|
||||
_SCROLL_BUTTONS = {
|
||||
Button.scroll_up.value: (0, 1),
|
||||
Button.scroll_down.value: (0, -1),
|
||||
Button.scroll_right.value: (1, 0),
|
||||
Button.scroll_left.value: (-1, 0)}
|
||||
|
||||
_EVENTS = (
|
||||
Xlib.X.ButtonPressMask,
|
||||
Xlib.X.ButtonReleaseMask)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Listener, self).__init__(*args, **kwargs)
|
||||
|
||||
def _handle_message(self, dummy_display, event, injected):
|
||||
px = event.root_x
|
||||
py = event.root_y
|
||||
|
||||
if event.type == Xlib.X.ButtonPress:
|
||||
# Scroll events are sent as button presses with the scroll
|
||||
# button codes
|
||||
scroll = self._SCROLL_BUTTONS.get(event.detail, None)
|
||||
if scroll:
|
||||
self.on_scroll(
|
||||
px, py, scroll[0], scroll[1], injected)
|
||||
else:
|
||||
self.on_click(
|
||||
px, py, self._button(event.detail), True, injected)
|
||||
|
||||
elif event.type == Xlib.X.ButtonRelease:
|
||||
# Send an event only if this was not a scroll event
|
||||
if event.detail not in self._SCROLL_BUTTONS:
|
||||
self.on_click(
|
||||
px, py, self._button(event.detail), False, injected)
|
||||
|
||||
else:
|
||||
self.on_move(px, py, injected)
|
||||
|
||||
|
||||
def _suppress_start(self, display):
|
||||
display.screen().root.grab_pointer(
|
||||
True, self._event_mask, Xlib.X.GrabModeAsync, Xlib.X.GrabModeAsync,
|
||||
0, 0, Xlib.X.CurrentTime)
|
||||
|
||||
def _suppress_stop(self, display):
|
||||
display.ungrab_pointer(Xlib.X.CurrentTime)
|
||||
|
||||
# pylint: disable=R0201
|
||||
def _button(self, detail):
|
||||
"""Creates a mouse button from an event detail.
|
||||
|
||||
If the button is unknown, :attr:`Button.unknown` is returned.
|
||||
|
||||
:param detail: The event detail.
|
||||
|
||||
:return: a button
|
||||
"""
|
||||
try:
|
||||
return Button(detail)
|
||||
except ValueError:
|
||||
return Button.unknown
|
||||
# pylint: enable=R0201
|
||||
Reference in New Issue
Block a user