def sendEvent(self, original, event): if not hasattr(self, 'app'): original(self, event) return # Keep track of an active Opt key if event.type() == NSFlagsChanged: flags = event.modifierFlags() self.app.toggle_key_active = (flags & NSAlternateKeyMask) and not (flags & NSControlKeyMask) # Handle reply/reply-all (XXX: won't work if you have assigned a different shortcut key to these actions) if self.app.toggle_key_active and event.type() == NSKeyDown and event.charactersIgnoringModifiers().lower() == 'r': # Strip the Opt-key from the event event = NSEvent.keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode_( event.type(), event.locationInWindow(), event.modifierFlags() & ~NSAlternateKeyMask, event.timestamp(), event.windowNumber(), event.context(), event.characters(), event.charactersIgnoringModifiers(), event.isARepeat(), event.keyCode() ) original(self, event)
def tkProcessKeyEvent(self, cls, theEvent): if self.acquire_state: if theEvent.type() == NSFlagsChanged: self.acquire_key = theEvent.modifierFlags( ) & NSDeviceIndependentModifierFlagsMask self.acquire_state = HotkeyMgr.ACQUIRE_NEW # suppress the event by not chaining the old function return theEvent elif theEvent.type() in (NSKeyDown, NSKeyUp): c = theEvent.charactersIgnoringModifiers() self.acquire_key = (c and ord(c[0]) or 0) | ( theEvent.modifierFlags() & NSDeviceIndependentModifierFlagsMask) self.acquire_state = HotkeyMgr.ACQUIRE_NEW # suppress the event by not chaining the old function return theEvent # replace empty characters with charactersIgnoringModifiers to avoid crash elif theEvent.type() in (NSKeyDown, NSKeyUp) and not theEvent.characters(): theEvent = NSEvent.keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode_( theEvent.type(), theEvent.locationInWindow(), theEvent.modifierFlags(), theEvent.timestamp(), theEvent.windowNumber(), theEvent.context(), theEvent.charactersIgnoringModifiers(), theEvent.charactersIgnoringModifiers(), theEvent.isARepeat(), theEvent.keyCode()) return self.tkProcessKeyEvent_old(cls, theEvent)
def createCocoaKeyEvent(keyCode, down=True): from AppKit import NSEvent, NSApplication, NSKeyDown, NSKeyUp, NSDate modifierFlags = 0 return NSEvent.keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode_( NSKeyDown if down else NSKeyUp, (0, 0), modifierFlags, NSDate.timeIntervalSinceReferenceDate(), #theEvent.timestamp(), 0, #theEvent.windowNumber(), None, # context None, # characters None, # charactersIgnoringModifiers False, # isARepeat keyCode # keyCode )
def tkProcessKeyEvent(self, cls, theEvent): if self.acquire_state: if theEvent.type() == NSFlagsChanged: self.acquire_key = theEvent.modifierFlags() & NSDeviceIndependentModifierFlagsMask self.acquire_state = HotkeyMgr.ACQUIRE_NEW # suppress the event by not chaining the old function return theEvent elif theEvent.type() in (NSKeyDown, NSKeyUp): c = theEvent.charactersIgnoringModifiers() self.acquire_key = (c and ord(c[0]) or 0) | (theEvent.modifierFlags() & NSDeviceIndependentModifierFlagsMask) self.acquire_state = HotkeyMgr.ACQUIRE_NEW # suppress the event by not chaining the old function return theEvent # replace empty characters with charactersIgnoringModifiers to avoid crash elif theEvent.type() in (NSKeyDown, NSKeyUp) and not theEvent.characters(): theEvent = NSEvent.keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode_(theEvent.type(), theEvent.locationInWindow(), theEvent.modifierFlags(), theEvent.timestamp(), theEvent.windowNumber(), theEvent.context(), theEvent.charactersIgnoringModifiers(), theEvent.charactersIgnoringModifiers(), theEvent.isARepeat(), theEvent.keyCode()) return self.tkProcessKeyEvent_old(cls, theEvent)
def sendEvent(self, original, event): if not hasattr(self, 'app'): original(self, event) return self.app.toggle_key_active = False # keep track of an active option key flags = event.modifierFlags() if (flags & NSAlternateKeyMask) and not (flags & NSControlKeyMask): self.app.toggle_key_active = True # handle reply/reply-all (XXX: won't work if you have assigned # a different shortcut key to these actions!) if event.type() == NSKeyDown and event.charactersIgnoringModifiers( ).lower() == 'r': # strip the Option-key from the event event = NSEvent.keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode_( event.type(), event.locationInWindow(), event.modifierFlags() & ~NSAlternateKeyMask, event.timestamp(), event.windowNumber(), event.context(), event.characters(), event.charactersIgnoringModifiers(), event.isARepeat(), event.keyCode()) original(self, event)