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 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 PressKeyOSX(hexKeyCode): global lastKey lastKey = hexKeyCode print("prepush") event = CoreGraphics.CGEventCreateKeyboardEvent(None, hexKeyCode, True) CoreGraphics.CGEventPost(CoreGraphics.kCGHIDEventTap, event) print("push")
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 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 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 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))
def ReleaseKeyOSX(): hexKeyCode = lastKey print("prerelease") event = CoreGraphics.CGEventCreateKeyboardEvent(None, hexKeyCode, False) CoreGraphics.CGEventPost(CoreGraphics.kCGHIDEventTap, event) print("release")