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)
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)
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)
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)
def PressKeyOSX(hexKeyCode): global lastKey lastKey = hexKeyCode print("prepush") event = CoreGraphics.CGEventCreateKeyboardEvent(None, hexKeyCode, True) CoreGraphics.CGEventPost(CoreGraphics.kCGHIDEventTap, event) print("push")
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)
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)
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))
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)
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))
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))
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
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))
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")
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))
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))
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)
def ReleaseKeyOSX(): hexKeyCode = lastKey print("prerelease") event = CoreGraphics.CGEventCreateKeyboardEvent(None, hexKeyCode, False) CoreGraphics.CGEventPost(CoreGraphics.kCGHIDEventTap, event) print("release")
def mouse_event(type, posx, posy): theEvent = cg.CGEventCreateMouseEvent(None, type, (posx, posy), cg.kCGMouseButtonLeft) cg.CGEventPost(cg.kCGHIDEventTap, theEvent)
def release_key(self, key_code, scancode): CG.CGEventPost( CG.kCGHIDEventTap, CG.CGEventCreateKeyboardEvent(None, key_code, False))
def press_key(self, key_code, scancode): CG.CGEventPost( CG.kCGHIDEventTap, CG.CGEventCreateKeyboardEvent(None, key_code, True))