macOS 中英文输入法切换终极解决方案

方便自己以后看,文章参考:https://www.geek-share.com/detail/2728080505.html

作为一个对工具有绝对掌控欲的强迫症程序员来说,无法得心应手的切换中英文输入法一直让我耿耿于怀。我想实现的是不用看通知栏就能知道当前输入法的状态,一键切换到自己需要的输入法,做到对输入法的绝对掌控。

具体问题

不喜欢自带的简体拼音,使用的是搜狗。搜狗自带英文模式,但有时候会误操作切换到 ABC,而 ABC 和搜狗之间切换的快捷键ctrl + space很不方便。

不同的程序需要不同的输入法。程序间的切换导致输入法的频繁切换,很多时候都会忘记切换输入法,增多错误输出,降低工作效率。

通知栏的输入法状态并不醒目,而且需要切换眼光焦点到通知栏才能获知当前输入法,打断工作思路。

解决方案

放弃搜狗的英文模式,使用 ABC + 搜狗。使用 InputSourceSelector + Keyboard Maestro 绑定中英文输入法到不同快捷键,使用 Karabiner 将快捷键映射到左右command按键上。

使用 Keyboard Pilot 为每个程序设置初始输入法。

使用 ShowyEdge 为不同的输入法设置不同的通知栏颜色。

绑定输入法到特定按键

InputSourceSelector安装

下载 InputSourceSelector 并解压到指定目录,例如app目录

启动终端,输入:cd ~/app/InputSourceSelector-master/&&./InputSourceSelector current

当输出当前的输入法则表明 InputSourceSelector 安装成功。

Karabiner-Elements 并安装

修改其配置文件,路径为~/.config/karabiner/karabiner.json,配置代码如下。将代码粘贴在”rules”: 后即可,该配置总共做了三项工作:
– 将 左Shift 键修改为 F17 键
– 将 左command 键修改为 F18 键
– 将 右command 键修改为 F19 键

{
    "global": {
        "check_for_updates_on_startup": true,
        "show_in_menu_bar": true,
        "show_profile_name_in_menu_bar": false
    },
    "profiles": [
        {
            "complex_modifications": {
                "parameters": {
                    "basic.simultaneous_threshold_milliseconds": 50,
                    "basic.to_delayed_action_delay_milliseconds": 500,
                    "basic.to_if_alone_timeout_milliseconds": 1000,
                    "basic.to_if_held_down_threshold_milliseconds": 500,
                    "mouse_motion_to_scroll.speed": 100
                },
                "rules": [
                    {
                        "description": "left_shift -- change input method",
                        "manipulators": [
                            {
                                "from": {
                                    "key_code": "left_shift",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "left_shift"
                                    }
                                ],
                                "to_if_alone": [
                                    {
                                        "key_code": "f17"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    },
                    {
                        "description": "left_command -- english input method",
                        "manipulators": [
                            {
                                "from": {
                                    "key_code": "left_command",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "left_command"
                                    }
                                ],
                                "to_if_alone": [
                                    {
                                        "key_code": "f18"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    },
                    {
                        "description": "right_command -- chinese input method",
                        "manipulators": [
                            {
                                "from": {
                                    "key_code": "right_command",
                                    "modifiers": {
                                        "optional": [
                                            "any"
                                        ]
                                    }
                                },
                                "to": [
                                    {
                                        "key_code": "right_command"
                                    }
                                ],
                                "to_if_alone": [
                                    {
                                        "key_code": "f19"
                                    }
                                ],
                                "type": "basic"
                            }
                        ]
                    }
                ]
            },
            "devices": [],
            "fn_function_keys": [
                {
                    "from": {
                        "key_code": "f1"
                    },
                    "to": {
                        "consumer_key_code": "display_brightness_decrement"
                    }
                },
                {
                    "from": {
                        "key_code": "f2"
                    },
                    "to": {
                        "consumer_key_code": "display_brightness_increment"
                    }
                },
                {
                    "from": {
                        "key_code": "f3"
                    },
                    "to": {
                        "key_code": "mission_control"
                    }
                },
                {
                    "from": {
                        "key_code": "f4"
                    },
                    "to": {
                        "key_code": "launchpad"
                    }
                },
                {
                    "from": {
                        "key_code": "f5"
                    },
                    "to": {
                        "key_code": "illumination_decrement"
                    }
                },
                {
                    "from": {
                        "key_code": "f6"
                    },
                    "to": {
                        "key_code": "illumination_increment"
                    }
                },
                {
                    "from": {
                        "key_code": "f7"
                    },
                    "to": {
                        "consumer_key_code": "rewind"
                    }
                },
                {
                    "from": {
                        "key_code": "f8"
                    },
                    "to": {
                        "consumer_key_code": "play_or_pause"
                    }
                },
                {
                    "from": {
                        "key_code": "f9"
                    },
                    "to": {
                        "consumer_key_code": "fastforward"
                    }
                },
                {
                    "from": {
                        "key_code": "f10"
                    },
                    "to": {
                        "consumer_key_code": "mute"
                    }
                },
                {
                    "from": {
                        "key_code": "f11"
                    },
                    "to": {
                        "consumer_key_code": "volume_decrement"
                    }
                },
                {
                    "from": {
                        "key_code": "f12"
                    },
                    "to": {
                        "consumer_key_code": "volume_increment"
                    }
                }
            ],
            "name": "Default profile",
            "parameters": {
                "delay_milliseconds_before_open_device": 1000
            },
            "selected": true,
            "simple_modifications": [],
            "virtual_hid_keyboard": {
                "caps_lock_delay_milliseconds": 0,
                "country_code": 0,
                "keyboard_type": "ansi",
                "mouse_key_xy_scale": 100
            }
        }
    ]
}

修改输入法切换方式

打开系统偏好设置—键盘—快捷键—输入法,双击选择上一个输入法的快捷键ctrl + space,按左shift,将其修改为F17

Keyboard Maestro 设置

添加英文切换宏

添加中文切换宏

说明:

直接使用 Karabiner 和 Keyboard Maestro 的切换输入法功能,出现间歇性失灵的问题,表现为输入法切换为中文了,但输出仍为英文。因此采用快捷键的方式切换输入法。

由于系统默认快捷键ctrl + space是组合按键,会出现间歇性无法切换的问题。因此修改快捷键为F17。

直接使用 Keyboard Maestro 中的JavaScript脚本判断当前输入法速度较慢,大概需要350ms。因此使用 InputSourceSelector 获取当前输入法。

发表评论

邮箱地址不会被公开。 必填项已用*标注