def netflix_keyboard(commands): commandList = dict(volup=([126], None, 1, False), voldown=([125], None, 1, False), pauseresume=([49], None, 1, False), forward=([124], None, 1, False), rewind=([123], None, 1, False), fullscreen=([3], None, 1, True), mute=([46], None, 1, False)) source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState) events = [] for cmd in commands: if cmd[1:] not in commandList: continue cmdProperties = commandList[cmd[1:]] if cmdProperties[3]: netflix_mouse(["c:640,400", "m:0,300"]) for i in range(cmdProperties[2]): for keyCode in cmdProperties[0]: down = CGEventCreateKeyboardEvent(source, keyCode, True) up = CGEventCreateKeyboardEvent(source, keyCode, False) flags = cmdProperties[1] if flags is not None: CGEventSetFlags(down, flags) events.append(down) events.append(up) for event in events: CGEventPost(kCGAnnotatedSessionEventTap, event)
def _send_string_press(c): event = CGEventCreateKeyboardEvent(OUTPUT_SOURCE, 0, True) KeyboardEmulation._set_event_string(event, c) CGEventPost(kCGSessionEventTap, event) event = CGEventCreateKeyboardEvent(OUTPUT_SOURCE, 0, False) KeyboardEmulation._set_event_string(event, c) CGEventPost(kCGSessionEventTap, event)
def send_string(self, s): for c in characters(s): event = CGEventCreateKeyboardEvent(MY_EVENT_SOURCE, 0, True) set_string(event, c) CGEventPost(kCGSessionEventTap, event) event = CGEventCreateKeyboardEvent(MY_EVENT_SOURCE, 0, False) set_string(event, c) CGEventPost(kCGSessionEventTap, event)
def send_backspaces(number_of_backspaces): for _ in xrange(number_of_backspaces): CGEventPost( kCGSessionEventTap, CGEventCreateKeyboardEvent(OUTPUT_SOURCE, BACK_SPACE, True)) CGEventPost( kCGSessionEventTap, CGEventCreateKeyboardEvent(OUTPUT_SOURCE, BACK_SPACE, False))
def send_backspaces(number_of_backspaces): for _ in range(number_of_backspaces): backspace_down = CGEventCreateKeyboardEvent( OUTPUT_SOURCE, BACK_SPACE, True) backspace_up = CGEventCreateKeyboardEvent(OUTPUT_SOURCE, BACK_SPACE, False) CGEventPost(kCGSessionEventTap, backspace_down) CGEventPost(kCGSessionEventTap, backspace_up)
def _send_string_press(self, c): event = CGEventCreateKeyboardEvent(OUTPUT_SOURCE, 0, True) KeyboardEmulation._set_event_string(event, c) CGEventPost(kCGSessionEventTap, event) event = CGEventCreateKeyboardEvent(OUTPUT_SOURCE, 0, False) KeyboardEmulation._set_event_string(event, c) CGEventPost(kCGSessionEventTap, event) if self._time_between_key_presses != 0: sleep(self._time_between_key_presses / 1000)
def send_backspaces(self, number_of_backspaces): for _ in range(number_of_backspaces): backspace_down = CGEventCreateKeyboardEvent( OUTPUT_SOURCE, BACK_SPACE, True) backspace_up = CGEventCreateKeyboardEvent( OUTPUT_SOURCE, BACK_SPACE, False) CGEventPost(kCGSessionEventTap, backspace_down) CGEventPost(kCGSessionEventTap, backspace_up) if self._time_between_key_presses != 0: sleep(self._time_between_key_presses / 1000)
def _send_sequence(self, sequence): # There is a bug in the event system that seems to cause inconsistent # modifiers on key events: # http://stackoverflow.com/questions/2008126/cgeventpost-possible-bug-when-simulating-keyboard-events # My solution is to manage the state myself. # I'm not sure how to deal with caps lock. # If event_mask is not zero at the end then bad things might happen. event_mask = 0 for keycode, key_down in sequence: if not key_down and keycode in MODIFIER_KEYS_TO_MASKS: event_mask &= ~MODIFIER_KEYS_TO_MASKS[keycode] event = CGEventCreateKeyboardEvent( MY_EVENT_SOURCE, keycode, key_down) CGEventSetFlags(event, event_mask) CGEventPost(kCGSessionEventTap, event) if key_down and keycode in MODIFIER_KEYS_TO_MASKS: event_mask |= MODIFIER_KEYS_TO_MASKS[keycode]
def _send_sequence(self, sequence): # There is a bug in the event system that seems to cause inconsistent # modifiers on key events: # http://stackoverflow.com/questions/2008126/cgeventpost-possible-bug-when-simulating-keyboard-events # My solution is to manage the state myself. # I'm not sure how to deal with caps lock. # If mods_flags is not zero at the end then bad things might happen. mods_flags = 0 for keycode, key_down in sequence: if keycode >= NX_KEY_OFFSET: # Handle media (NX) key. event = KeyboardEmulation._get_media_event( keycode - NX_KEY_OFFSET, key_down) else: # Handle regular keycode. if not key_down and keycode in MODIFIER_KEYS_TO_MASKS: mods_flags &= ~MODIFIER_KEYS_TO_MASKS[keycode] if key_down and keycode_needs_fn_mask(keycode): mods_flags |= kCGEventFlagMaskSecondaryFn event = CGEventCreateKeyboardEvent( OUTPUT_SOURCE, keycode, key_down) if key_down and keycode not in MODIFIER_KEYS_TO_MASKS: event_flags = CGEventGetFlags(event) # Add wanted flags, remove unwanted flags. goal_flags = ((event_flags & ~KeyboardEmulation.MODS_MASK) | mods_flags) if event_flags != goal_flags: CGEventSetFlags(event, goal_flags) # Half millisecond pause after key down. sleep(0.0005) if key_down and keycode in MODIFIER_KEYS_TO_MASKS: mods_flags |= MODIFIER_KEYS_TO_MASKS[keycode] if not key_down and keycode_needs_fn_mask(keycode): mods_flags &= ~kCGEventFlagMaskSecondaryFn CGEventPost(kCGSessionEventTap, event) if self._time_between_key_presses != 0: sleep(self._time_between_key_presses / 1000)
def send_backspaces(self, number_of_backspaces): for _ in xrange(number_of_backspaces): CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(MY_EVENT_SOURCE, 51, True)) CGEventPost(kCGSessionEventTap, CGEventCreateKeyboardEvent(MY_EVENT_SOURCE, 51, False))