~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to mago/mouse/windows.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
 
 
3
#   Copyright 2010 Pepijn de Vos
 
4
#
 
5
#   Licensed under the Apache License, Version 2.0 (the "License");
 
6
#   you may not use this file except in compliance with the License.
 
7
#   You may obtain a copy of the License at
 
8
#
 
9
#       http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
#   Unless required by applicable law or agreed to in writing, software
 
12
#   distributed under the License is distributed on an "AS IS" BASIS,
 
13
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
#   See the License for the specific language governing permissions and
 
15
#   limitations under the License.
 
16
 
 
17
clicks = [None, 2, 8, 32]
 
18
releases = [None, 4, 16, 64]
 
19
 
 
20
from ctypes import *
 
21
from win32api import GetSystemMetrics
 
22
from base import PyMouseMeta, PyMouseEventMeta
 
23
import pythoncom, pyHook
 
24
from time import sleep
 
25
 
 
26
PUL = POINTER(c_ulong)
 
27
class MouseInput(Structure):
 
28
    _fields_ = [("dx", c_long),
 
29
             ("dy", c_long),
 
30
             ("mouseData", c_ulong),
 
31
             ("dwFlags", c_ulong),
 
32
             ("time",c_ulong),
 
33
             ("dwExtraInfo", PUL)]
 
34
 
 
35
class Input_I(Union):
 
36
    _fields_ = [("mi", MouseInput)]
 
37
 
 
38
class Input(Structure):
 
39
    _fields_ = [("type", c_ulong), ("ii", Input_I)]
 
40
 
 
41
FInputs = Input * 2
 
42
extra = c_ulong(0)
 
43
 
 
44
click = Input_I()
 
45
click.mi = MouseInput(0, 0, 0, 2, 0, pointer(extra))
 
46
release = Input_I()
 
47
release.mi = MouseInput(0, 0, 0, 4, 0, pointer(extra))
 
48
 
 
49
blob = FInputs( (0, click), (0, release) )
 
50
 
 
51
class POINT(Structure):
 
52
    _fields_ = [("x", c_ulong),
 
53
                ("y", c_ulong)]
 
54
 
 
55
class PyMouse(PyMouseMeta):
 
56
    def press(self, x, y, button = 1):
 
57
        self.move(x, y)
 
58
        blob[0].ii.mi.dwFlags = clicks[button]
 
59
        windll.user32.SendInput(2,pointer(blob),sizeof(blob[0]))
 
60
 
 
61
    def release(self, x, y, button = 1):
 
62
        self.move(x, y)
 
63
        blob[1].ii.mi.dwFlags = releases[button]
 
64
        windll.user32.SendInput(2,pointer(blob),sizeof(blob[0]))
 
65
 
 
66
    def move(self, x, y):
 
67
        windll.user32.SetCursorPos(x, y)
 
68
 
 
69
    def position(self):
 
70
        pt = POINT()
 
71
        windll.user32.GetCursorPos(byref(pt))
 
72
        return pt.x, pt.y
 
73
 
 
74
    def screen_size(self):
 
75
        width = GetSystemMetrics(0)
 
76
        height = GetSystemMetrics(1)
 
77
        return width, height
 
78
 
 
79
class PyMouseEvent(PyMouseEventMeta):
 
80
    def run(self):
 
81
        hm = pyHook.HookManager()
 
82
        hm.MouseAllButtons = self._click
 
83
        hm.MouseMove = self._move
 
84
        hm.HookMouse()
 
85
 
 
86
        while self.state:
 
87
            sleep(0.01)
 
88
            pythoncom.PumpWaitingMessages()
 
89
 
 
90
    def _click(self, event):
 
91
        x,y = event.Position
 
92
 
 
93
        if event.Message == pyHook.HookConstants.WM_LBUTTONDOWN:
 
94
            self.click(x, y, 1, True)
 
95
        elif event.Message == pyHook.HookConstants.WM_LBUTTONUP:
 
96
            self.click(x, y, 1, False)
 
97
        elif event.Message == pyHook.HookConstants.WM_RBUTTONDOWN:
 
98
            self.click(x, y, 2, True)
 
99
        elif event.Message == pyHook.HookConstants.WM_RBUTTONUP:
 
100
            self.click(x, y, 2, False)
 
101
        elif event.Message == pyHook.HookConstants.WM_MBUTTONDOWN:
 
102
            self.click(x, y, 3, True)
 
103
        elif event.Message == pyHook.HookConstants.WM_MBUTTONUP:
 
104
            self.click(x, y, 3, False)
 
105
        return not self.capture
 
106
 
 
107
    def _move(self, event):
 
108
        x,y = event.Position
 
109
        self.move(x, y)
 
110
        return not self.capture