之所以有这个需求,源于我刚刚换了macbook,习惯了在win下面按esc退出微信窗口以及qq窗口,然而mac下只能够用command+w,实在习惯不了。目前有两个软件方案可以实现,将esc映射成command+w的需求。首先是收费的BetterTouchTool,功能非常多,支持键盘映射只是小菜一碟,然而收费,而且还不便宜。因此我选择了第二个方案karabiner-element(Karabiner-Elements is a powerful utility for keyboard customization on macOS Sierra (10.12) or later.)。
在安装好软件之后,先打开karabiner-element软件,在线添加个规则 complex modifications->Rules->Add rule,比如vim映射之类的,hjkl。然后产生了配置文件之后,在
~/.config/karabinerkarabiner.json里面添加我们的规则就行了。
具体的规则是添加在profiles.rules数组下既可,
{
"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": "esc->command+w",
"manipulators": [
{
"conditions": [
{
"bundle_identifiers": [
"^com\\.apple\\.finder$",
"^com\\.tencent\\.xinWeChat$",
"^com\\.tencent\\.qq$"
],
"type": "frontmost_application_if"
}
],
"from": {
"key_code": "escape"
},
"to": [
{
"key_code": "w",
"modifiers": [
"left_command"
]
}
],
"type": "basic"
}
]
}
稍微解释下,上述的规则就是from 从esc映射 to 到左command+w。生效的话,则是在bundle_identifiers数组里的软件才能够生效,软件的名字,可以在karabiner-eventViewer程序中可以看得到,更详细的可以看文档。https://karabiner-elements.pqrs.org/docs/.