def get_layout_spec(self): layout = "us" layouts = ["us"] variant = "" variants = [] try: from AppKit import NSTextInputContext #@UnresolvedImport ic = NSTextInputContext.new() current_keyboard = ic.selectedKeyboardInputSource() code = APPLE_LAYOUTS.get(current_keyboard.split(".")[-1]) log("get_layout_spec() current_keyboard=%s, code=%s", current_keyboard, code) all_keyboards = ic.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() layout=%s, layouts=%s", 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
def watch(ime): txtObj = NSTextInputContext.alloc().initWithClient_(NSTextView.new()) obs = Observer.new().initWithValue_(txtObj) obs.ime = ime Foundation.NSNotificationCenter.defaultCenter().addObserver_selector_name_object_( obs, 'bundle:', NSTextInputContextKeyboardSelectionDidChangeNotification, None) AppHelper.runConsoleEventLoop()
def __init__(self) -> None: self.text_input_context = NSTextInputContext.alloc().initWithClient_(NSTextView.new()) self.last_ime = ImSwitcher.english_ime manager = multiprocessing.Manager() self.cur_ime = manager.Value(ctypes.c_wchar_p, ImSwitcher.english_ime) multiprocessing.Process(target=Observer.watch, args=(self.cur_ime,)).start() try: # 隐藏macos dock栏小火箭 info = NSBundle.mainBundle().infoDictionary() info["LSBackgroundOnly"] = "1" except ImportError: print("隐藏macos dock栏小火箭,需要pip3 install -U PyObjC")
def __init__(self, vim): self.vim = vim self.text_input_context = NSTextInputContext.alloc().initWithClient_( NSTextView.new())