def interruptwait(): """ If waituntil() has been called, this will interrupt the waiting process so it can check whether it should stop waiting. """ evt = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(NSApplicationDefined, NSPoint(), NSApplicationDefined, 0, 1, None, LIGHTBLUE_NOTIFY_ID, 0, 0) NSApplication.sharedApplication().postEvent_atStart_(evt, True)
def release(self, key_code): """ Sends an 'up' event for the specified scan code """ if key_code >= 128: # Media key ev = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_( 14, # type (0, 0), # location 0xb00, # flags 0, # timestamp 0, # window 0, # ctx 8, # subtype ((key_code - 128) << 16) | (0xb << 8), # data1 -1 # data2 ) Quartz.CGEventPost(0, ev.CGEvent()) else: # Regular key # Update modifiers if necessary if key_code == 0x37: # cmd self.current_modifiers["cmd"] = False elif key_code == 0x38: # shift self.current_modifiers["shift"] = False elif key_code == 0x39: # caps lock self.current_modifiers["caps"] = False elif key_code == 0x3A: # alt self.current_modifiers["alt"] = False elif key_code == 0x3B: # ctrl self.current_modifiers["ctrl"] = False # Apply modifiers if necessary event_flags = 0 if self.current_modifiers["shift"]: event_flags += Quartz.kCGEventFlagMaskShift if self.current_modifiers["caps"]: event_flags += Quartz.kCGEventFlagMaskAlphaShift if self.current_modifiers["alt"]: event_flags += Quartz.kCGEventFlagMaskAlternate if self.current_modifiers["ctrl"]: event_flags += Quartz.kCGEventFlagMaskControl if self.current_modifiers["cmd"]: event_flags += Quartz.kCGEventFlagMaskCommand event = Quartz.CGEventCreateKeyboardEvent(None, key_code, False) Quartz.CGEventSetFlags(event, event_flags) Quartz.CGEventPost(Quartz.kCGHIDEventTap, event) time.sleep(0.01)
def _press_special_key(self, key, down): """ Helper method for special keys. Source: http://stackoverflow.com/questions/11045814/emulate-media-key-press-on-mac """ key_code = special_key_translate_table[key] ev = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_( NSSystemDefined, # type (0,0), # location 0xa00 if down else 0xb00, # flags 0, # timestamp 0, # window 0, # ctx 8, # subtype (key_code << 16) | ((0xa if down else 0xb) << 8), # data1 -1 # data2 ) CGEventPost(0, ev.CGEvent())
def _press_special_key(self, key, down): """ Helper method for special keys. Source: http://stackoverflow.com/questions/11045814/emulate-media-key-press-on-mac """ key_code = special_key_translate_table[key] ev = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_( NSSystemDefined, # type (0,0), # location 0xa00 if down else 0xb00, # flags 0, # timestamp 0, # window 0, # ctx 8, # subtype (key_code << 16) | ((0xa if down else 0xb) << 8), # data1 -1 # data2 ) Quartz.CGEventPost(0, ev.Quartz.CGEvent())
def kickRunLoop(self): event = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(NSApplicationDefined, \ NSMakePoint(0,0), 0, 0.0, 0, None, 0, 0, 0) NSApp.postEvent_atStart_(event, True)