def _get_symbol_and_modifiers(ev): # The unicode char help processing virtual keycodes (see issue 405) wchar = c_wchar() carbon.GetEventParameter(ev, kEventParamKeyUnicodes, typeUnicodeText, c_void_p(), sizeof(wchar), c_void_p(), byref(wchar)) try: wchar = str((wchar.value)).upper() except UnicodeEncodeError: # (this fix for issue 405 caused a bug itself (see comments 6-7); # this try/except fixes it) wchar = None # If the unicode char is within charmap keys (ascii value), then we use # the corresponding symbol. if wchar in charmap.keys(): symbol = charmap[wchar] else: sym = c_uint32() carbon.GetEventParameter(ev, kEventParamKeyCode, typeUInt32, c_void_p(), sizeof(sym), c_void_p(), byref(sym)) symbol = keymap.get(sym.value, None) if symbol is None: symbol = key.user_key(sym.value) modifiers = c_uint32() carbon.GetEventParameter(ev, kEventParamKeyModifiers, typeUInt32, c_void_p(), sizeof(modifiers), c_void_p(), byref(modifiers)) return (symbol, CarbonWindow._map_modifiers(modifiers.value))
def getSymbol(nsevent): symbol = keymap.get(nsevent.keyCode(), None) if symbol is not None: return symbol chars = cocoapy.cfstring_to_string(nsevent.charactersIgnoringModifiers()) if chars: return charmap.get(chars[0].upper(), None) return None
def pygletFlagsChanged_(self, nsevent): # Handles on_key_press and on_key_release events for modifier keys. # Note that capslock is handled differently than other keys; it acts # as a toggle, so on_key_release is only sent when it's turned off. # TODO: Move these constants somewhere else. # Undocumented left/right modifier masks found by experimentation: NSLeftShiftKeyMask = 1 << 1 NSRightShiftKeyMask = 1 << 2 NSLeftControlKeyMask = 1 << 0 NSRightControlKeyMask = 1 << 13 NSLeftAlternateKeyMask = 1 << 5 NSRightAlternateKeyMask = 1 << 6 NSLeftCommandKeyMask = 1 << 3 NSRightCommandKeyMask = 1 << 4 maskForKey = { key.LSHIFT: NSLeftShiftKeyMask, key.RSHIFT: NSRightShiftKeyMask, key.LCTRL: NSLeftControlKeyMask, key.RCTRL: NSRightControlKeyMask, key.LOPTION: NSLeftAlternateKeyMask, key.ROPTION: NSRightAlternateKeyMask, key.LCOMMAND: NSLeftCommandKeyMask, key.RCOMMAND: NSRightCommandKeyMask, key.CAPSLOCK: cocoapy.NSAlphaShiftKeyMask, key.FUNCTION: cocoapy.NSFunctionKeyMask } symbol = keymap.get(nsevent.keyCode(), None) # Ignore this event if symbol is not a modifier key. We must check this # because e.g., we receive a flagsChanged message when using CMD-tab to # switch applications, with symbol == "a" when command key is released. if symbol is None or symbol not in maskForKey: return modifiers = getModifiers(nsevent) modifierFlags = nsevent.modifierFlags() if symbol and modifierFlags & maskForKey[symbol]: self._window.dispatch_event('on_key_press', symbol, modifiers) else: self._window.dispatch_event('on_key_release', symbol, modifiers)