Skip to content

Commit

Permalink
Merge pull request #114 from harry5263/master
Browse files Browse the repository at this point in the history
Add drag and drop function to AXClasses.py
  • Loading branch information
nagappan committed Jan 6, 2015
2 parents 64d4710 + ff79667 commit faabd76
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions atomac/AXClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,75 @@ def _queueMouseButton(self, coord, mouseButton, modFlags, clickCount=1, dest_coo
self._queueEvent(Quartz.CGEventPost,
(Quartz.kCGSessionEventTap, buttonUp))

def _leftMouseDragged(self, stopCoord, strCoord, speed):
''' Private method to handle generic mouse left button dragging and
dropping
Parameters: stopCoord(x,y) drop point
Optional: strCoord (x, y) drag point, default (0,0) get current
mouse position
speed (int) 1 to unlimit, simulate mouse moving
action from some special requirement
Returns: None
'''
# To direct output to the correct application need the PSN:
appPsn = self._getPsnForPid(self._getPid())
# Get current position as start point if strCoord not given
if strCoord == (0,0):
loc = AppKit.NSEvent.mouseLocation()
strCoord = (loc.x, Quartz.CGDisplayPixelsHigh(0)-loc.y)

# To direct output to the correct application need the PSN:
appPsn = self._getPsnForPid(self._getPid())

# Press left button down
pressLeftButton = Quartz.CGEventCreateMouseEvent(None,
Quartz.kCGEventLeftMouseDown,
strCoord,
Quartz.kCGMouseButtonLeft)
# Queue the events
Quartz.CGEventPost(Quartz.CoreGraphics.kCGHIDEventTap, pressLeftButton)
# Wait for reponse of system, a fuzzy icon appears
time.sleep(5)
# Simulate mouse moving speed, k is slope
speed = round(1/float(speed), 2)
xmoved = stopCoord[0]-strCoord[0]
ymoved = stopCoord[1]-strCoord[1]
if ymoved == 0:
k = 0
raise ValueError('Not support horizontal moving')
else:
k = abs(ymoved/xmoved)

if xmoved != 0:
for xpos in range(int(abs(xmoved))):
if xmoved > 0 and ymoved > 0:
currcoord = (strCoord[0]+xpos, strCoord[1]+xpos*k)
elif xmoved > 0 and ymoved < 0:
currcoord = (strCoord[0]+xpos, strCoord[1]-xpos*k)
elif xmoved < 0 and ymoved < 0:
currcoord = (strCoord[0]-xpos, strCoord[1]-xpos*k)
elif xmoved < 0 and ymoved > 0:
currcoord = (strCoord[0]-xpos, strCoord[1]+xpos*k)
# Drag with left button
dragLeftButton = Quartz.CGEventCreateMouseEvent(None,
Quartz.kCGEventLeftMouseDragged,
currcoord,
Quartz.kCGMouseButtonLeft)
Quartz.CGEventPost(Quartz.CoreGraphics.kCGHIDEventTap, dragLeftButton)
# Wait for reponse of system
time.sleep(speed)
else:
raise ValueError('Not support vertical moving')
upLeftButton = Quartz.CGEventCreateMouseEvent(None,
Quartz.kCGEventLeftMouseUp,
stopCoord,
Quartz.kCGMouseButtonLeft)
# Wait for reponse of system, a plus icon appears
time.sleep(5)
# Up left button up
Quartz.CGEventPost(Quartz.CoreGraphics.kCGHIDEventTap, upLeftButton)

def _waitFor(self, timeout, notification, **kwargs):
'''waitFor - Wait for a particular UI event to occur; this can be built
upon in NativeUIElement for specific convenience methods.
Expand Down Expand Up @@ -1015,6 +1084,17 @@ def clickMouseButtonRightWithMods(self, coord, modifiers):
self._releaseModifiers(modifiers)
self._postQueuedEvents()

def leftMouseDragged(self, stopCoord, strCoord=(0,0), speed=1):
''' Click the left mouse button and drag object
Parameters: stopCoord, the position of dragging stopped
strCoord, the position of dragging started
(0,0) will get current position
speed is mouse moving speed, 0 to unlimited
Returns: None
'''
self._leftMouseDragged(stopCoord, strCoord, speed)

def doubleClickMouse(self, coord):
''' Double-click primary mouse button
Expand Down

0 comments on commit faabd76

Please sign in to comment.