def process_key_event(self, send_key_action_cb, wid, key_event): if self.swap_keys: trans = self.key_translations.get(key_event.keyname) if trans: log("swap keys: translating key '%s' to %s", key_event, trans) key_event.keycode, key_event.keyname = trans if key_event.keycode==self.num_lock_keycode and not key_event.pressed: log("toggling numlock") self.num_lock_state = not self.num_lock_state getOSXMenuHelper().update_numlock(self.num_lock_state) send_key_action_cb(wid, key_event)
def set_modifier_mappings(self, mappings): KeyboardBase.set_modifier_mappings(self, mappings) self.meta_modifier = self.modifier_keys.get("Meta_L") or self.modifier_keys.get("Meta_R") self.control_modifier = self.modifier_keys.get("Control_L") or self.modifier_keys.get("Control_R") self.num_lock_modifier = self.modifier_keys.get("Num_Lock") log("set_modifier_mappings(%s) meta=%s, control=%s, numlock=%s", mappings, self.meta_modifier, self.control_modifier, self.num_lock_modifier) #find the keysyms and keycodes to use for each key we may translate: for orig_keysym in KEYS_TRANSLATION_OPTIONS.keys(): new_def = self.find_translation(orig_keysym) if new_def is not None: self.key_translations[orig_keysym] = new_def log("set_modifier_mappings(..) swap keys translations=%s", self.key_translations)
def mask_to_names(self, mask): names = KeyboardBase.mask_to_names(self, mask) if self.swap_keys and self.meta_modifier is not None and self.control_modifier is not None: meta_on = bool(mask & META_MASK) meta_set = self.meta_modifier in names control_set = self.control_modifier in names log("mask_to_names names=%s, meta_on=%s, meta_set=%s, control_set=%s", names, meta_on, meta_set, control_set) if meta_on and not control_set: log("mask_to_names swapping meta for control: %s for %s", self.meta_modifier, self.control_modifier) names.append(self.control_modifier) if meta_set: names.remove(self.meta_modifier) elif control_set and not meta_on: log("mask_to_names swapping control for meta: %s for %s", self.control_modifier, self.meta_modifier) names.remove(self.control_modifier) if not meta_set: names.append(self.meta_modifier) #deal with numlock: if self.num_lock_modifier is not None: if self.num_lock_state and self.num_lock_modifier not in names: names.append(self.num_lock_modifier) elif not self.num_lock_state and self.num_lock_modifier in names: names.remove(self.num_lock_modifier) log("mask_to_names(%s)=%s", mask, names) return names
def find_translation(self, keysyms): log("find_translation(%s)", keysyms) new_def = None #ie: keysyms : ["Meta_L", "Alt_L"] for keysym in keysyms: #ie: "Alt_L": keycodes_defs = self.modifier_keycodes.get(keysym) log("modifier_keycodes(%s)=%s", keysym, keycodes_defs) if not keycodes_defs: #keysym not found continue #ie: [(55, 'Alt_L'), (58, 'Alt_L'), 'Alt_L'] for keycode_def in keycodes_defs: if type(keycode_def) == str: #ie: 'Alt_L' #no keycode found, but better than nothing: new_def = 0, keycode_def #ie: (0, 'Alt_L') continue #an int alone is the keycode: if type(keycode_def) == int: if keycode_def > 0: #exact match, use it: return keycode_def, keysym new_def = 0, keysym continue #below is for compatibility with older servers, #(we may be able to remove some of this code already) #look for a tuple of (keycode, keysym): if type(keycode_def) not in (list, tuple) or len(keycode_def) != 2: continue if type(keycode_def[0]) != int or type(keycode_def[1]) != str: continue if keycode_def[0] == 0: new_def = keycode_def continue #found a valid keycode, use this one: return keycode_def #ie: (55, 'Alt_L') return new_def
def mask_to_names(self, mask): names = KeyboardBase.mask_to_names(self, mask) log( "mask_to_names names=%s, meta mod=%s, control mod=%s, num lock mod=%s, num lock state=%s", names, self.meta_modifier, self.control_modifier, self.num_lock_modifier, self.num_lock_state) if self.swap_keys and self.meta_modifier is not None and self.control_modifier is not None: #clear both: for x in (self.control_modifier, self.meta_modifier): if x in names: names.remove(x) #re-add as needed: if mask & META_MASK: names.append(self.control_modifier) if mask & CONTROL_MASK: names.append(self.meta_modifier) #deal with numlock: if self.num_lock_modifier is not None: if self.num_lock_state and self.num_lock_modifier not in names: names.append(self.num_lock_modifier) elif not self.num_lock_state and self.num_lock_modifier in names: names.remove(self.num_lock_modifier) log("mask_to_names(%s)=%s", mask, names) return names
def get_layout_spec(self): layout = "us" layouts = ["us"] variant = "" variants = [] options = "" try: from AppKit import NSTextInputContext, NSTextView #@UnresolvedImport text_input_context = NSTextInputContext.new() view = NSTextView.new() text_input_context.initWithClient_(view) current_keyboard = text_input_context.selectedKeyboardInputSource() code = APPLE_LAYOUTS.get(current_keyboard.split(".")[-1]) log("get_layout_spec() current_keyboard=%s, code=%s", current_keyboard, code) all_keyboards = text_input_context.keyboardInputSources() log("get_layout_spec() other keyboards=%s", all_keyboards) if code: layout = code if all_keyboards: layouts = [] for k in all_keyboards: code = APPLE_LAYOUTS.get(k.split(".")[-1]) if code: layouts.append(code) if not layouts: layouts.append("us") else: if code not in layouts: layouts.insert(0, code) log( "get_layout_spec() view=%s, input_context=%s, layout=%s, layouts=%s", view, text_input_context, layout, layouts) except Exception as e: log("get_layout_spec()", exc_info=True) log.error("Error querying keyboard layout:") log.error(" %s", e) return layout, layouts, variant, variants, options
def mask_to_names(self, mask): names = KeyboardBase.mask_to_names(self, mask) if self.swap_keys and self.meta_modifier is not None and self.control_modifier is not None: meta_on = bool(mask & gtk.gdk.META_MASK) meta_set = self.meta_modifier in names control_set = self.control_modifier in names if meta_on and not control_set: log("mask_to_names swapping meta for control: %s for %s", self.meta_modifier, self.control_modifier) names.append(self.control_modifier) if meta_set: names.remove(self.meta_modifier) elif control_set and not meta_on: log("mask_to_names swapping control for meta: %s for %s", self.control_modifier, self.meta_modifier) names.remove(self.control_modifier) if not meta_set: names.append(self.meta_modifier) #deal with numlock: if self.num_lock_modifier is not None: if self.num_lock_state and self.num_lock_modifier not in names: names.append(self.num_lock_modifier) elif not self.num_lock_state and self.num_lock_modifier in names: names.remove(self.num_lock_modifier) log("mask_to_names(%s)=%s", mask, names) return names