File: util_cocoa.py

package info (click to toggle)
python-pywebview 3.3.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 29,536 kB
  • sloc: python: 5,703; javascript: 888; cs: 130; sh: 55; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,347 bytes parent folder | download | duplicates (2)
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
"""
Cocoa window cannot be destroyed programmatically, until it finishes processing a NSEvent
So we need to simulate a mouse movement in order to generate an event.
"""

from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventCreate
from Quartz.CoreGraphics import CGEventGetLocation
from Quartz.CoreGraphics import CFRelease
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGEventMouseMoved
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap


def mousePos():
    event = CGEventCreate(None)
    pointer = CGEventGetLocation(event)
    #CFRelease(event)
    return pointer.x, pointer.y


def mouseEvent(type, posx, posy):
    theEvent = CGEventCreateMouseEvent(None, type, (posx, posy), kCGMouseButtonLeft)
    CGEventPost(kCGHIDEventTap, theEvent)


def mouseMove(posx, posy):
    mousePos()
    mouseEvent(kCGEventMouseMoved, posx, posy)


def mouseMoveRelative(dx, dy):
    posx, posy = mousePos()
    mouseMove(posx + dx, posy + dy)


def mouseclick(posx, posy):
    mouseEvent(kCGEventLeftMouseDown, posx, posy)
    mouseEvent(kCGEventLeftMouseUp, posx, posy)


if __name__ == '__main__':
    mouseMoveRelative(100, 100)