我是来用 https://karabiner-elements.pqrs.org/ 来做按键绑定
可以参考我的配置中关于 RIME 的配置(搜RIME即可)
dotfiles/karabiner.json at main · jixiuf/dotfiles · GitHub
比如这段,在非emacs 的app 上按下right_shift 的行为(通过frontmost_application_unless
断言实现判断)
- 当单right_shift时 发送
left_control+space
即 系统切换输入法的按键
- 当right_shift 与其他键组合按时,依然是shift的功能,如shift+a=A
{
"conditions": [
{
"bundle_identifiers": [
"^org\\.gnu\\.Emacs$",
"^org\\.gnu\\.AquamacsEmacs$",
"^org\\.gnu\\.Aquamacs$"
],
"file_paths": [
"/usr/local/emacs/bin/emacs",
"/usr/local/bin/emacs"
],
"type": "frontmost_application_unless"
}
],
"description": "按 right_command 切换系统英文和 RIME 输入法",
"from": {
"key_code": "right_shift",
"modifiers": {
"optional": [
"caps_lock"
]
}
},
"to": [
{
"key_code": "right_shift"
}
],
"to_if_alone": [
{
"key_code": "spacebar",
"modifiers": [
"left_control"
]
}
],
"type": "basic"
},
下面这一段的行为 当input_sources=en 且当前app 是emacs时right_shift的行为
- right_shift+a 等组合按键时 依然拥有right_shift原生的功能
- 当right_shift 单独按下抬起时,相当于按下left_control+space ,后 再按下f18
而我emacs f18的按键绑定是进入evil-insert-state,
意思是说当前处于英文态时按时right_shift 会通过left_control+space切换到中文输入法,同时进入evil-insert-state
(global-set-key (kbd "<f18>") 'evil-insert-state) ;mac karabiner用来控制输入法
{
"conditions": [
{
"input_sources": [
{
"language": "en"
}
],
"type": "input_source_if"
},
{
"bundle_identifiers": [
"^org\\.gnu\\.Emacs$",
"^org\\.gnu\\.AquamacsEmacs$",
"^org\\.gnu\\.Aquamacs$"
],
"file_paths": [
"/usr/local/emacs/bin/emacs",
"/usr/local/bin/emacs"
],
"type": "frontmost_application_if"
}
],
"description": "emacs按 right_command 切换到RIME 输入法",
"from": {
"key_code": "right_shift",
"modifiers": {
"optional": [
"caps_lock"
]
}
},
"to": [
{
"key_code": "right_shift"
}
],
"to_if_alone": [
{
"key_code": "spacebar",
"modifiers": [
"left_control"
]
},
{
"key_code": "f18"
}
],
"type": "basic"
},
下面这段的意思是 非英文状态下 emacs中按下right_shift的行为
单按right_shift后,先后发送f19, left_control+space 两个按键序列
f19 我在rime 输入法中 绑定为Send Escape 用于清空输入法中未上屏的输入
key_binder/bindings:
# Ctrl-g
# - { when: composing, accept: Control+g, send: Escape}
- { when: composing, accept: F19, send: Escape }
(global-set-key (kbd "<f19>") #'ignore) ;mac karabiner用来控制输入法 ,rime f19 send escape
(define-key isearch-mode-map (kbd "<f19>") #'ignore) ;详见isearch-pre-command-hook
{
"conditions": [
{
"input_sources": [
{
"language": "en"
}
],
"type": "input_source_unless"
},
{
"bundle_identifiers": [
"^org\\.gnu\\.Emacs$",
"^org\\.gnu\\.AquamacsEmacs$",
"^com\\.googlecode\\.iterm2$",
"^org\\.gnu\\.Aquamacs$"
],
"file_paths": [
"/usr/local/emacs/bin/emacs",
"/usr/local/bin/emacs"
],
"type": "frontmost_application_if"
}
],
"description": "emacs按 right_command 切换到英文输入法",
"from": {
"key_code": "right_shift",
"modifiers": {
"optional": [
"caps_lock"
]
}
},
"to": [
{
"key_code": "right_shift"
}
],
"to_if_alone": [
{
"key_code": "f19"
},
{
"key_code": "spacebar",
"modifiers": [
"left_control"
]
}
],
"type": "basic"
},
像input_sources 、bundle_identifiers 等 可以通过它提供的工具来查看,只需要你切换到相应的app ,它就会显示相应的值,如图
还有其他一些功能,如
我将spacebar 当成hyper来用 hyper 被我定义为 (cmd+ctrl+option)
而space+e = toggle emacs (即当前窗口若是eamcs ,则space+e则隐藏当前窗口,若不是当前窗口,则将emacs 挪到最上层)
更多功能参见我的配置吧。