Ejemplo n.º 1
0
    def double_click(self, x, y, button_name=LEFT_BUTTON, click_interval=0.5):
        MacUtils.verify_xy_coordinates(x, y)
        MacUtils.verify_mouse_button_name(button_name,
                                          self._SUPPORTED_BUTTON_NAMES)

        if button_name == self.LEFT_BUTTON:
            button = CG.kCGMouseButtonLeft
            down = CG.kCGEventLeftMouseDown
            up = CG.kCGEventLeftMouseUp
        if button_name == self.RIGHT_BUTTON:
            button = CG.kCGMouseButtonRight
            down = CG.kCGEventRightMouseDown
            up = CG.kCGEventRightMouseUp

        # http://www.codeitive.com/0iJqgkejVj/performing-a-double-click-using-cgeventcreatemouseevent.html
        event = CG.CGEventCreateMouseEvent(None, down, (x, y), button)
        CG.CGEventPost(CG.kCGSessionEventTap, event)
        CG.CGEventSetType(event, up)
        CG.CGEventPost(CG.kCGSessionEventTap, event)

        CG.CGEventSetIntegerValueField(event, CG.kCGMouseEventClickState, 2)
        # https://msdn.microsoft.com/en-us/library/windows/desktop/ms646263%28v=vs.85%29.aspx
        sleep(click_interval)

        CG.CGEventSetType(event, down)
        CG.CGEventPost(CG.kCGSessionEventTap, event)
        CG.CGEventSetType(event, up)
        CG.CGEventPost(CG.kCGSessionEventTap, event)
Ejemplo n.º 2
0
    def double_click(self, x, y, button_name=LEFT_BUTTON):
        MacUtils.verify_xy_coordinates(x, y)
        MacUtils.verify_mouse_button_name(button_name,
                                          self._SUPPORTED_BUTTON_NAMES)

        if button_name == self.LEFT_BUTTON:
            button = CG.kCGMouseButtonLeft
            down = CG.kCGEventLeftMouseDown
            up = CG.kCGEventLeftMouseUp
        if button_name == self.RIGHT_BUTTON:
            button = CG.kCGMouseButtonRight
            down = CG.kCGEventRightMouseDown
            up = CG.kCGEventRightMouseUp

        # http://www.codeitive.com/0iJqgkejVj/performing-a-double-click-using-cgeventcreatemouseevent.html
        event = CG.CGEventCreateMouseEvent(None, down, (x, y), button)
        CG.CGEventPost(CG.kCGHIDEventTap, event)
        CG.CGEventSetType(event, up)
        CG.CGEventPost(CG.kCGHIDEventTap, event)

        CG.CGEventSetIntegerValueField(event, CG.kCGMouseEventClickState, 2)

        CG.CGEventSetType(event, down)
        CG.CGEventPost(CG.kCGHIDEventTap, event)
        CG.CGEventSetType(event, up)
        CG.CGEventPost(CG.kCGHIDEventTap, event)
Ejemplo n.º 3
0
    def sendKey(self, key, modifiers=0x0):

        source = CG.CGEventSourceCreate(CG.kCGEventSourceStateCombinedSessionState)

        keyDown = CG.CGEventCreateKeyboardEvent(source, key, True)
        CG.CGEventSetFlags(keyDown, modifiers)
        keyUp = CG.CGEventCreateKeyboardEvent(source, key, False)

        CG.CGEventPost(CG.kCGAnnotatedSessionEventTap, keyDown)
        CG.CGEventPost(CG.kCGAnnotatedSessionEventTap, keyUp)
Ejemplo n.º 4
0
    def double_click(self, x, y, delay=DELAY_IN_MOUSE_CLICK):
        event = self._mouse_event(self.buttons['left']['down_event'], x, y,
                                  self.buttons['left']['button'])
        CG.CGEventSetType(event, self.buttons['left']['up_event'])
        CG.CGEventPost(CG.kCGHIDEventTap, event)

        CG.CGEventSetIntegerValueField(event, CG.kCGMouseEventClickState, 2)

        CG.CGEventSetType(event, self.buttons['left']['down_event'])
        CG.CGEventPost(CG.kCGHIDEventTap, event)
        CG.CGEventSetType(event, self.buttons['left']['up_event'])
        CG.CGEventPost(CG.kCGHIDEventTap, event)
Ejemplo n.º 5
0
def PressKeyOSX(hexKeyCode):
    global lastKey
    lastKey = hexKeyCode
    print("prepush")
    event = CoreGraphics.CGEventCreateKeyboardEvent(None, hexKeyCode, True)
    CoreGraphics.CGEventPost(CoreGraphics.kCGHIDEventTap, event)
    print("push")
Ejemplo n.º 6
0
 def fake_key(self, keycode, press):
     log.info("fake_key(%s, %s)", keycode, press)
     e = CG.CGEventCreateKeyboardEvent(None, keycode, press)
     #CGEventSetFlags(keyPress, modifierFlags)
     #modifierFlags: kCGEventFlagMaskShift, ...
     CG.CGEventPost(CG.kCGSessionEventTap, e)
     CG.CFRelease(e)
Ejemplo n.º 7
0
    def click_mouse(self):
        point = CG.CGPointMake(self.width / 2, 250)
        # Move mouse to top-middle position.
        move = CG.CGEventCreateMouseEvent(None, CG.kCGEventMouseMoved, point,
                                          CG.kCGMouseButtonLeft)
        # Mouse down.
        down = CG.CGEventCreateMouseEvent(NULL, CG.kCGEventLeftMouseDown,
                                          point, CG.kCGMouseButtonLeft)
        # Mouse up.
        up = CG.CGEventCreateMouseEvent(NULL, CG.kCGEventLeftMouseUp, point,
                                        CG.kCGMouseButtonLeft)

        #send the events
        CG.CGEventPost(CG.kCGHIDEventTap, move)
        CG.CGEventPost(CG.kCGHIDEventTap, down)
        time.sleep(0.05)
        CG.CGEventPost(CG.kCGHIDEventTap, up)
Ejemplo n.º 8
0
    def release_key(self, hex_key_code):
        """
        Releases key specified by a hex code.

        :param int hex_key_code: hexadecimal code for a key to be pressed.
        """
        CG.CGEventPost(
            CG.kCGHIDEventTap,
            CG.CGEventCreateKeyboardEvent(None, hex_key_code, False))
Ejemplo n.º 9
0
 def move_mouse(self):
     x = random.randint(0, self.width)
     y = random.randint(0, self.height)
     #create the event
     move = CG.CGEventCreateMouseEvent(None, CG.kCGEventMouseMoved,
                                       CG.CGPointMake(x, y),
                                       CG.kCGMouseButtonLeft)
     #send the event
     CG.CGEventPost(CG.kCGHIDEventTap, move)
Ejemplo n.º 10
0
    def press_key_and_hold(self, hex_key_code):
        """
        Presses (and holds) key specified by a hex code.

        :param int hex_key_code: hexadecimal code for a key to be pressed.
        """
        CG.CGEventPost(
            CG.kCGHIDEventTap,
            CG.CGEventCreateKeyboardEvent(None, hex_key_code, True))
Ejemplo n.º 11
0
    def press_key_and_hold(self, hex_key_code):
        """Presses (and holds) key specified by a hex code.

        Arguments:
            - hex_key_code: integer value holding hexadecimal code for a key to
            be pressed.
        Returns:
            - None
        """

        CG.CGEventPost(CG.kCGHIDEventTap,
                       CG.CGEventCreateKeyboardEvent(None, hex_key_code, True))
Ejemplo n.º 12
0
    def _mouse_event(self,
                     event_type,
                     x,
                     y,
                     mouse_button=CG.kCGMouseButtonLeft):
        if mouse_button not in [CG.kCGMouseButtonLeft, CG.kCGMouseButtonRight]:
            raise TypeError('Pikuli.MacMouse: incorrect mouse button type')

        evt = CG.CGEventCreateMouseEvent(None, event_type, (x, y),
                                         mouse_button)
        CG.CGEventPost(CG.kCGHIDEventTap, evt)
        return evt
Ejemplo n.º 13
0
    def release_key(self, hex_key_code):
        """Releases key specified by a hex code.

        Arguments:
            - hex_key_code: integer value holding hexadecimal code for a key to
            be pressed.
        Returns:
            - None
        """

        CG.CGEventPost(
            CG.kCGHIDEventTap,
            CG.CGEventCreateKeyboardEvent(None, hex_key_code, False))
Ejemplo n.º 14
0
 def event(self, key: str, down: bool):
     """ Create a Core Graphics keyboard event and post it for macOS to process """
     try:
         event = CG.CGEventCreateKeyboardEvent(None,
                                               self.KeyMap[key.lower()],
                                               down)
         if key.isupper():
             CG.CGEventSetFlags(
                 event,
                 CG.kCGEventFlagMaskShift | CG.CGEventGetFlags(event))
         CG.CGEventPost(CG.kCGHIDEventTap, event)
     except KeyError:
         raise NotImplementedError(f"Key '{key}' is not implemented")
Ejemplo n.º 15
0
    def _do_event(self, code, x, y):
        """
        Generates mouse event for a special coordinate.

        :param int code: mouse event code.
        :param int x: x coordinate.
        :param int y: y coordinate.
        """
        if code in self._LEFT_BUTTON_CODES:
            button = CG.kCGMouseButtonLeft
        elif code in self._RIGHT_BUTTON_CODES:
            button = CG.kCGMouseButtonRight
        else:
            button = CG.kCGMouseButtonCenter

        CG.CGEventPost(CG.kCGHIDEventTap,
                       CG.CGEventCreateMouseEvent(None, code, (x, y), button))
Ejemplo n.º 16
0
    def _do_event(self, code, x, y):
        """
        Generates mouse event for a special coordinate.

        Arguments:
            - code: integer value holding mouse event code.
            - x: integer value with x coordinate.
            - y: integer value with y coordinate.
        Returns:
            - None
        """

        if code in self._LEFT_BUTTON_CODES:
            button = CG.kCGMouseButtonLeft
        elif code in self._RIGHT_BUTTON_CODES:
            button = CG.kCGMouseButtonRight
        else:
            button = CG.kCGMouseButtonCenter

        CG.CGEventPost(CG.kCGHIDEventTap,
                       CG.CGEventCreateMouseEvent(None, code, (x, y), button))
Ejemplo n.º 17
0
 def _scroll_event(direction=1):
     if direction not in [-1, 1]:
         raise TypeError('Pikuli.MacMouse: incorrect scroll direction type')
     evt = CG.CGEventCreateScrollWheelEvent(None, 0, 1, int(direction))
     CG.CGEventPost(CG.kCGHIDEventTap, evt)
Ejemplo n.º 18
0
def ReleaseKeyOSX():
    hexKeyCode = lastKey
    print("prerelease")
    event = CoreGraphics.CGEventCreateKeyboardEvent(None, hexKeyCode, False)
    CoreGraphics.CGEventPost(CoreGraphics.kCGHIDEventTap, event)
    print("release")
Ejemplo n.º 19
0
def mouse_event(type, posx, posy):
    theEvent = cg.CGEventCreateMouseEvent(None, type, (posx, posy),
                                          cg.kCGMouseButtonLeft)
    cg.CGEventPost(cg.kCGHIDEventTap, theEvent)
Ejemplo n.º 20
0
 def release_key(self, key_code, scancode):
     CG.CGEventPost(
         CG.kCGHIDEventTap,
         CG.CGEventCreateKeyboardEvent(None, key_code, False))
Ejemplo n.º 21
0
 def press_key(self, key_code, scancode):
     CG.CGEventPost(
         CG.kCGHIDEventTap,
         CG.CGEventCreateKeyboardEvent(None, key_code, True))