-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSX_mouse_controller.py
More file actions
106 lines (74 loc) · 3.1 KB
/
OSX_mouse_controller.py
File metadata and controls
106 lines (74 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGEventMouseMoved
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventRightMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGEventRightMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap, kCGEventLeftMouseDragged, kCGEventSourceStateHIDSystemState
from Quartz.CoreGraphics import CGEventCreate
from Quartz.CoreGraphics import CGEventGetLocation
from Quartz.CoreGraphics import CGEventSourceCreate
from AppKit import NSScreen, NSEvent
class Mouse():
"""a class containing attributes and methods needed to controlling mouse cursor
"""
def __init__(self):
self.maxX = int(NSScreen.mainScreen().frame().size.width)
self.maxY = int(NSScreen.mainScreen().frame().size.height)
self.moveBy(0,0)
def __mouseEvent(self, type, x, y):
"""creates and posts mouse event of given type at (posx, posy) position"""
theEvent = CGEventCreateMouseEvent(CGEventSourceCreate(kCGEventSourceStateHIDSystemState), type, (x, y), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def moveTo(self, x, y):
"""moves mouse cursor to (x,y) position"""
if x < 0:
self.x = 0
elif x > self.maxX:
self.x = self.maxX
else:
self.x = x
if y < 0:
self.y = 0
elif y > self.maxY:
self.y = self.maxY
else:
self.y = y
print self.x, self.y
self.__mouseEvent(kCGEventMouseMoved, self.x, self.y)
self.__mouseEvent(kCGEventLeftMouseDragged, self.x, self.y)
def moveBy(self, x, y):
"""moves coursor by x step and y step"""
ourEvent = CGEventCreate(None);
mouseLocation = CGEventGetLocation(ourEvent);
self.moveTo(int(mouseLocation.x) + x, int(mouseLocation.y) + y)
def leftButtonDown(self):
"""makes left button of mouse pushed"""
self.__mouseEvent(kCGEventLeftMouseDown, self.x, self.y)
def leftButtonUp(self):
"""makes left button of mouse unpushed"""
self.__mouseEvent(kCGEventLeftMouseUp, self.x, self.y)
def leftClick(self):
"""makes "left click" """
self.leftButtonDown()
self.leftButtonUp()
def doubleLeftClick(self):
""" makes double left Click """
self.leftClick()
self.leftClick()
def rightButtonDown(self):
"""makes right button of mouse pushed"""
self.__mouseEvent(kCGEventRightMouseDown, self.x, self.y)
def rightButtonUp(self):
"""makes right button of mouse unpushed"""
self.__mouseEvent(kCGEventRightMouseUp, self.x, self.y)
def rightClick(self):
"""makes "right click" """
self.rightButtonDown()
self.rightButtonUp()
def doubleRightClick(self):
""" makes double right Click """
self.rightClick()
self.rightClick()