Skip to content

Commit b181ac4

Browse files
committed
added mouse controller tutorial
1 parent 92ff1a1 commit b181ac4

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2626
- [How to Make Facebook Messenger bot in Python](https://www.thepythoncode.com/article/make-bot-fbchat-python). ([code](general/messenger-bot))
2727
- [How to Transfer Files in the Network using Sockets in Python](https://www.thepythoncode.com/article/send-receive-files-using-sockets-python). ([code](general/transfer-files/))
2828
- [How to Get Hardware and System Information in Python](https://www.thepythoncode.com/article/get-hardware-system-information-python). ([code](general/sys-info))
29+
- [How to Control your Mouse in Python](https://www.thepythoncode.com/article/control-mouse-python). ([code](general/mouse-controller))
2930

general/mouse-controller/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [How to Control your Mouse in Python](https://www.thepythoncode.com/article/control-mouse-python)
2+
The script `control_mouse.py` is suggested to be run on an interactive python shell, such as ipython, jupyter lab, etc.
3+
Install requirements:
4+
- `pip3 install -r requirements.txt`
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import mouse
2+
3+
# left click
4+
mouse.click('left')
5+
6+
# right click
7+
mouse.click('right')
8+
9+
# middle click
10+
mouse.click('middle')
11+
12+
# get the position of mouse
13+
print(mouse.get_position())
14+
# In [12]: mouse.get_position()
15+
# Out[12]: (714, 488)
16+
17+
# presses but doesn't release
18+
mouse.hold('left')
19+
# mouse.press('left')
20+
21+
# drag from (0, 0) to (100, 100) relatively with a duration of 0.1s
22+
mouse.drag(0, 0, 100, 100, absolute=False, duration=0.1)
23+
24+
# whether a button is clicked
25+
print(mouse.is_pressed('right'))
26+
27+
# move 100 right & 100 down
28+
mouse.move(100, 100, absolute=False, duration=0.2)
29+
30+
# make a listener when left button is clicked
31+
mouse.on_click(lambda: print("Left Button clicked."))
32+
# make a listener when right button is clicked
33+
mouse.on_right_click(lambda: print("Right Button clicked."))
34+
35+
# remove the listeners when you want
36+
mouse.unhook_all()
37+
38+
# scroll down
39+
mouse.wheel(-1)
40+
41+
# scroll up
42+
mouse.wheel(1)
43+
44+
# record until you click right
45+
events = mouse.record()
46+
47+
# replay these events
48+
mouse.play(events[:-1])
49+
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mouse

0 commit comments

Comments
 (0)