/mouse

Hook and simulate global mouse events in pure Python

Primary LanguagePythonMIT LicenseMIT

mouse

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.

Features

  • Global event hook on all mice devices (captures events regardless of focus).
  • Listen and sends mouse events.
  • Works with Windows and Linux (requires sudo).
  • Pure Python, no C modules to be compiled.
  • Zero dependencies. 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.

Usage

Install the PyPI package:

$ sudo pip install mouse

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.

Known limitations:

  • 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 requries 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.

API

Table of Contents

ButtonEvent(event_type, button, time)

Alias for field number 1

T.count(value) -> integer -- return number of occurrences of value

Alias for field number 0

T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.

Alias for field number 2

= 'double'
= 'down'
= 'left'
= 'middle'

MoveEvent(x, y, time)

T.count(value) -> integer -- return number of occurrences of value

T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.

Alias for field number 2

Alias for field number 0

Alias for field number 1

= 'right'
= 'up'

WheelEvent(delta, time)

T.count(value) -> integer -- return number of occurrences of value

Alias for field number 0

T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.

Alias for field number 1

= 'x'
= 'x2'

[source]

Returns True if the given button is currently pressed.

[source]

Presses the given button (but doesn't release).

Alias for press.

[source]

Releases the given button.

[source]

Sends a click with the given button.

[source]

Sends a double click with the given button.

[source]

Sends a right click with the given button.

[source]

Scrolls the wheel delta clicks. Sign indicates direction.

[source]

Moves the mouse. If absolute, to position (x, y), otherwise move relative to the current position. If duration is non-zero, animates the movement.

[source]

Holds the left mouse button, moving from start to end position, then releases. absolute and duration are parameters regarding the mouse movement.

[source]

Invokes callback with args when the specified event happens.

[source]

Invokes callback with args when the left button is clicked.

[source]

Invokes callback with args when the left button is double clicked.

[source]

Invokes callback with args when the right button is clicked.

[source]

Invokes callback with args when the middle button is clicked.

[source]

Blocks program execution until the given button performs an event.

[source]

Returns the (x, y) mouse position.

[source]

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.

[source]

Removes a previously installed hook.

[source]

Removes all hooks registered by this application. Note this may include hooks installed by high level functions, such as record.

[source]

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.

[source]

Plays a sequence of recorded events, maintaining the relative time intervals. If speed_factor is <= 0 then the actions are replayed as fast as the OS allows. Pairs well with record().

The parameters include_* define if events of that type should be inluded in the replay or ignored.

Alias for play.