Home

Awesome

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

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:

API

Table of Contents

<a name="mouse.ButtonEvent"/>

class mouse.ButtonEvent

ButtonEvent(event_type, button, time)

<a name="ButtonEvent.button"/>

ButtonEvent.button

Alias for field number 1

<a name="ButtonEvent.count"/>

ButtonEvent.count(self, value, /)

Return number of occurrences of value.

<a name="ButtonEvent.event_type"/>

ButtonEvent.event_type

Alias for field number 0

<a name="ButtonEvent.index"/>

ButtonEvent.index(self, value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

<a name="ButtonEvent.time"/>

ButtonEvent.time

Alias for field number 2

<a name="mouse.DOUBLE"/>

mouse.DOUBLE

= 'double'
<a name="mouse.DOWN"/>

mouse.DOWN

= 'down'
<a name="mouse.LEFT"/>

mouse.LEFT

= 'left'
<a name="mouse.MIDDLE"/>

mouse.MIDDLE

= 'middle'
<a name="mouse.MoveEvent"/>

class mouse.MoveEvent

MoveEvent(x, y, time)

<a name="MoveEvent.count"/>

MoveEvent.count(self, value, /)

Return number of occurrences of value.

<a name="MoveEvent.index"/>

MoveEvent.index(self, value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

<a name="MoveEvent.time"/>

MoveEvent.time

Alias for field number 2

<a name="MoveEvent.x"/>

MoveEvent.x

Alias for field number 0

<a name="MoveEvent.y"/>

MoveEvent.y

Alias for field number 1

<a name="mouse.RIGHT"/>

mouse.RIGHT

= 'right'
<a name="mouse.UP"/>

mouse.UP

= 'up'
<a name="mouse.WheelEvent"/>

class mouse.WheelEvent

WheelEvent(delta, time)

<a name="WheelEvent.count"/>

WheelEvent.count(self, value, /)

Return number of occurrences of value.

<a name="WheelEvent.delta"/>

WheelEvent.delta

Alias for field number 0

<a name="WheelEvent.index"/>

WheelEvent.index(self, value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

<a name="WheelEvent.time"/>

WheelEvent.time

Alias for field number 1

<a name="mouse.X"/>

mouse.X

= 'x'
<a name="mouse.X2"/>

mouse.X2

= 'x2'
<a name="mouse.version"/>

mouse.version

= '0.7.1'
<a name="mouse.is_pressed"/>

mouse.is_pressed(button='left')

[source]

Returns True if the given button is currently pressed.

<a name="mouse.press"/>

mouse.press(button='left')

[source]

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

<a name="mouse.release"/>

mouse.release(button='left')

[source]

Releases the given button.

<a name="mouse.click"/>

mouse.click(button='left')

[source]

Sends a click with the given button.

<a name="mouse.double_click"/>

mouse.double_click(button='left')

[source]

Sends a double click with the given button.

<a name="mouse.right_click"/>

mouse.right_click()

[source]

Sends a right click with the given button.

<a name="mouse.wheel"/>

mouse.wheel(delta=1)

[source]

Scrolls the wheel delta clicks. Sign indicates direction.

<a name="mouse.move"/>

mouse.move(x, y, absolute=True, duration=0, steps_per_second=120.0)

[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. The fps of the animation is determined by 'steps_per_second', default is 120.

<a name="mouse.drag"/>

mouse.drag(start_x, start_y, end_x, end_y, absolute=True, duration=0)

[source]

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

<a name="mouse.on_button"/>

mouse.on_button(callback, args=(), buttons=('left', 'middle', 'right', 'x', 'x2'), types=('up', 'down', 'double'))

[source]

Invokes callback with args when the specified event happens.

<a name="mouse.on_click"/>

mouse.on_click(callback, args=())

[source]

Invokes callback with args when the left button is clicked.

<a name="mouse.on_double_click"/>

mouse.on_double_click(callback, args=())

[source]

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

<a name="mouse.on_right_click"/>

mouse.on_right_click(callback, args=())

[source]

Invokes callback with args when the right button is clicked.

<a name="mouse.on_middle_click"/>

mouse.on_middle_click(callback, args=())

[source]

Invokes callback with args when the middle button is clicked.

<a name="mouse.wait"/>

mouse.wait(button='left', target_types=('up', 'down', 'double'))

[source]

Blocks program execution until the given button performs an event.

<a name="mouse.get_position"/>

mouse.get_position()

[source]

Returns the (x, y) mouse position.

<a name="mouse.hook"/>

mouse.hook(callback)

[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.

<a name="mouse.unhook"/>

mouse.unhook(callback)

[source]

Removes a previously installed hook.

<a name="mouse.unhook_all"/>

mouse.unhook_all()

[source]

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

<a name="mouse.record"/>

mouse.record(button='right', target_types=('down',))

[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.

<a name="mouse.play"/>

mouse.play(events, speed_factor=1.0, include_clicks=True, include_moves=True, include_wheel=True)

[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 included in the replay or ignored.