Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
The goal of PyMouse is to have a cross-platform way to control the mouse.
PyMouse should work on Windows, Mac and any Unix that has xlib.
PyMouse has been merged into PyUserInput at https://github.com/SavinaRoja/PyUserInput
58 changes: 29 additions & 29 deletions pymouse/mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,60 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from Quartz import *
import Quartz
from AppKit import NSEvent
from base import PyMouseMeta, PyMouseEventMeta

pressID = [None, kCGEventLeftMouseDown, kCGEventRightMouseDown, kCGEventOtherMouseDown]
releaseID = [None, kCGEventLeftMouseUp, kCGEventRightMouseUp, kCGEventOtherMouseUp]
pressID = [None, Quartz.kCGEventLeftMouseDown, Quartz.kCGEventRightMouseDown, Quartz.kCGEventOtherMouseDown]
releaseID = [None, Quartz.kCGEventLeftMouseUp, Quartz.kCGEventRightMouseUp, Quartz.kCGEventOtherMouseUp]

class PyMouse(PyMouseMeta):
def press(self, x, y, button = 1):
event = CGEventCreateMouseEvent(None, pressID[button], (x, y), button - 1)
CGEventPost(kCGHIDEventTap, event)
event = Quartz.CGEventCreateMouseEvent(None, pressID[button], (x, y), button - 1)
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)

def release(self, x, y, button = 1):
event = CGEventCreateMouseEvent(None, releaseID[button], (x, y), button - 1)
CGEventPost(kCGHIDEventTap, event)
event = Quartz.CGEventCreateMouseEvent(None, releaseID[button], (x, y), button - 1)
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)

def move(self, x, y):
move = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (x, y), 0)
CGEventPost(kCGHIDEventTap, move)
move = Quartz.CGEventCreateMouseEvent(None, Quartz.kCGEventMouseMoved, (x, y), 0)
Quartz.CGEventPost(Quartz.kCGHIDEventTap, move)


def position(self):
loc = NSEvent.mouseLocation()
return loc.x, CGDisplayPixelsHigh(0) - loc.y
return loc.x, Quartz.CGDisplayPixelsHigh(0) - loc.y

def screen_size(self):
return CGDisplayPixelsWide(0), CGDisplayPixelsHigh(0)
return Quartz.CGDisplayPixelsWide(0), Quartz.CGDisplayPixelsHigh(0)

class PyMouseEvent(PyMouseEventMeta):
def run(self):
tap = CGEventTapCreate(
kCGSessionEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
CGEventMaskBit(kCGEventMouseMoved) |
CGEventMaskBit(kCGEventLeftMouseDown) |
CGEventMaskBit(kCGEventLeftMouseUp) |
CGEventMaskBit(kCGEventRightMouseDown) |
CGEventMaskBit(kCGEventRightMouseUp) |
CGEventMaskBit(kCGEventOtherMouseDown) |
CGEventMaskBit(kCGEventOtherMouseUp),
tap = Quartz.CGEventTapCreate(
Quartz.kCGSessionEventTap,
Quartz.kCGHeadInsertEventTap,
Quartz.kCGEventTapOptionDefault,
Quartz.CGEventMaskBit(Quartz.kCGEventMouseMoved) |
Quartz.CGEventMaskBit(Quartz.kCGEventLeftMouseDown) |
Quartz.CGEventMaskBit(Quartz.kCGEventLeftMouseUp) |
Quartz.CGEventMaskBit(Quartz.kCGEventRightMouseDown) |
Quartz.CGEventMaskBit(Quartz.kCGEventRightMouseUp) |
Quartz.CGEventMaskBit(Quartz.kCGEventOtherMouseDown) |
Quartz.CGEventMaskBit(Quartz.kCGEventOtherMouseUp),
self.handler,
None)

loopsource = CFMachPortCreateRunLoopSource(None, tap, 0)
loop = CFRunLoopGetCurrent()
CFRunLoopAddSource(loop, loopsource, kCFRunLoopDefaultMode)
CGEventTapEnable(tap, True)
loopsource = Quartz.CFMachPortCreateRunLoopSource(None, tap, 0)
loop = Quartz.CFRunLoopGetCurrent()
Quartz.CFRunLoopAddSource(loop, loopsource, Quartz.kCFRunLoopDefaultMode)
Quartz.CGEventTapEnable(tap, True)

while self.state:
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 5, False)
Quartz.CFRunLoopRunInMode(Quartz.kCFRunLoopDefaultMode, 5, False)

def handler(self, proxy, type, event, refcon):
(x, y) = CGEventGetLocation(event)
(x, y) = Quartz.CGEventGetLocation(event)
if type in pressID:
self.click(x, y, pressID.index(type), True)
elif type in releaseID:
Expand All @@ -76,6 +76,6 @@ def handler(self, proxy, type, event, refcon):
self.move(x, y)

if self.capture:
CGEventSetType(event, kCGEventNull)
Quartz.CGEventSetType(event, Quartz.kCGEventNull)

return event
4 changes: 2 additions & 2 deletions pymouse/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def position(self):
return pt.x, pt.y

def screen_size(self):
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
width = windll.user32.GetSystemMetrics(0)
height = windll.user32.GetSystemMetrics(1)
return width, height

class PyMouseEvent(PyMouseEventMeta):
Expand Down
42 changes: 42 additions & 0 deletions tests/test_import_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# coding=utf8
# Copyright 2014 Christopher H. Casebeer
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''
Tests that import time for pymouse is not unreasonably long.

This catches the slow-import-of-Quartz on Mac OS X due to pyobjc's slow
behavior when import via from ... import *.
'''

import contextlib
import time

from nose import SkipTest

@contextlib.contextmanager
def check_execution_time(description, max_seconds):
start_time = time.time()
yield
end_time = time.time()
execution_time = end_time - start_time
if execution_time > max_seconds:
raise Exception("Took too long to complete %s" % description)

def test_pymouse_import_time():
# skip this test by default – call nosetests with --no-skip to enable
raise SkipTest()
with check_execution_time("importing pymouse", max_seconds=3):
import pymouse