Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Hook and simulate global mouse events in pure Python
License
boppreh/mouse
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
In order to avoid highly increased duration on slower machines caused by the movement logic itself and the inconsistency of the sleep, move the mouse based on the elapsed time and consider the above when sleeping to keep the fps cap
Git stats
Files
Failed to load latest commit information.
README.md
Take full control of your mouse with this small Python library. Hook global events, register hotkeys, simulate mouse movement and clicks, and much more.
Huge thanks to Kirill Pavlov for donating the package name. If you are looking for the Cheddargetter.com client implementation, pip install mouse==0.5.0 .
- Global event hook on all mice devices (captures events regardless of focus).
- Listen and sends mouse events.
- Works with Windows and Linux (requires sudo).
- Works with MacOS (requires granting accessibility permissions to terminal/python in System Preferences -> Security & Privacy)
- Pure Python, no C modules to be compiled.
- Zero dependencies on Windows and Linux. Trivial to install and deploy, just copy the files.
- Python 2 and 3.
- Includes high level API (e.g. record and play.
- Events automatically captured in separate thread, doesn’t block main program.
- Tested and documented.
This program makes no attempt to hide itself, so don’t use it for keyloggers.
or clone the repository (no installation required, source files are sufficient):
$ git clone https://github.com/boppreh/mouse
Then check the API docs to see what features are available.
- Events generated under Windows don’t report device id ( event.device == None ). #21
- To avoid depending on X the Linux parts reads raw device files ( /dev/input/input* ) but this requires root.
- Other applications, such as some games, may register hooks that swallow all key events. In this case mouse will be unable to report events.
class mouse.ButtonEvent
ButtonEvent(event_type, button, time)
ButtonEvent.button
ButtonEvent.count(self, value, /)
Return number of occurrences of value.
ButtonEvent.event_type
ButtonEvent.index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
ButtonEvent.time
mouse.DOUBLE
mouse.MIDDLE
class mouse.MoveEvent
MoveEvent.count(self, value, /)
Return number of occurrences of value.
MoveEvent.index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
MoveEvent.time
MoveEvent.x
MoveEvent.y
mouse.RIGHT
class mouse.WheelEvent
WheelEvent.count(self, value, /)
Return number of occurrences of value.
WheelEvent.delta
WheelEvent.index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
WheelEvent.time
mouse.version
mouse.is_pressed(button=’left’)
Returns True if the given button is currently pressed.
mouse.press(button=’left’)
Presses the given button (but doesn’t release).
mouse.release(button=’left’)
Releases the given button.
mouse.click(button=’left’)
Sends a click with the given button.
mouse.double_click(button=’left’)
Sends a double click with the given button.
mouse.right_click()
Sends a right click with the given button.
mouse.wheel(delta=1)
Scrolls the wheel delta clicks. Sign indicates direction.
mouse.move(x, y, absolute=True, duration=0, steps_per_second=120.0)
Moves the mouse. If absolute , to position (x, y), otherwise move relative to the current position. If duration is non-zero, animates the movement. The fps of the animation is determined by ‘steps_per_second’, default is 120.
mouse.drag(start_x, start_y, end_x, end_y, absolute=True, duration=0)
Holds the left mouse button, moving from start to end position, then releases. absolute and duration are parameters regarding the mouse movement.
mouse.on_button(callback, args=(), buttons=(‘left’, ‘middle’, ‘right’, ‘x’, ‘x2’), types=(‘up’, ‘down’, ‘double’))
Invokes callback with args when the specified event happens.
mouse.on_click(callback, args=())
Invokes callback with args when the left button is clicked.
mouse.on_double_click(callback, args=())
Invokes callback with args when the left button is double clicked.
mouse.on_right_click(callback, args=())
Invokes callback with args when the right button is clicked.
mouse.on_middle_click(callback, args=())
Invokes callback with args when the middle button is clicked.
mouse.wait(button=’left’, target_types=(‘up’, ‘down’, ‘double’))
Blocks program execution until the given button performs an event.
mouse.get_position()
Returns the (x, y) mouse position.
mouse.hook(callback)
Installs a global listener on all available mouses, invoking callback each time it is moved, a key status changes or the wheel is spun. A mouse event is passed as argument, with type either mouse.ButtonEvent , mouse.WheelEvent or mouse.MoveEvent .
Returns the given callback for easier development.
mouse.unhook(callback)
Removes a previously installed hook.
mouse.unhook_all()
Removes all hooks registered by this application. Note this may include hooks installed by high level functions, such as record .
mouse.record(button=’right’, target_types=(‘down’,))
Records all mouse events until the user presses the given button. Then returns the list of events recorded. Pairs well with play(events) .
Note: this is a blocking function. Note: for more details on the mouse hook and events see hook .
mouse.play(events, speed_factor=1.0, include_clicks=True, include_moves=True, include_wheel=True)
Plays a sequence of recorded events, maintaining the relative time intervals. If speed_factor is
The parameters include_* define if events of that type should be included in the replay or ignored.
About
Hook and simulate global mouse events in pure Python