Mac OS 应用级别按键转换

摘要:本文介绍在 Mac 系统下如何解决不同应用 Ctrl 和 Command 按键的自由切换。

众所周知,Mac 系统的独特的键位设计给需要在不同的操作系统之间切换的人来说实在是痛苦。小小的一个 Ctrl 和 Command 让人无比难受。这里我们使用 Karabiner 优雅的解决这个问题。

使用场景

本人经常在 Windows 和 Mac 之间切换使用,因为 Mac 散热问题,我总是喜欢把 CPU 繁重的任务丢给 Windows 机器,而且一般把 Windows 机器放远一点,通过 Microsoft 提供的高端 RDP 应用远程登录。 (微软的这个 Mac 版的 RDP 应用真心良心之作,能够在 RDP 中使用所有屏幕这一项就比 VNC 好很多)。

这样基本就是在 Mac 和 Windows 上穿插使用,再加上偶尔 VNC 到远程的 Fedora,基本三个系统都会涉及到。

在操作的过程中莫过于按键不一致带来了很多不便。其中最关键的就是 Ctrl 和 Command,再加上使用外接的机械 PC 键盘,感觉操作起来经常误操作。

解决思路

  1. 将 Mac 的 Control 和 Command 对调。这样首先很多快捷键就和 Windows 上一致了,比如拷贝和粘贴。
  2. 安装 Karabinar 可以解决大部分外接键盘布局在 Mac 系统下不准确的问题
  3. 安装一个第三方的组件,让 Karabinar 支持动态为不同的应用使用不同的 Profile
  4. 配置 RDP 软件独立的按键修改方案。

Karabinar 支持不同应用自动加载不同 Profile

在 GitHub 上有一个 issue 专门讨论这个问题,其中有一个人自己写了一个应用 Karabinar Elements App Profile 解决了这个问题。

讨论中给出了多种解决方法,这里给出一种最方便的实现

安装运行相关服务

1
2
3
brew cask install karabiner-elements
brew install https://raw.githubusercontent.com/toonetown/homebrew-extras/master/Formula/karabiner-elements-app-profiles.rb
brew services start karabiner-elements-app-profiles

如果已经安装了最新的 karabiner 就不需要第一步了。按照讨论中的说法,需要 Log Off 在 Log In 才能生效。

I ended up building this myself tonight — it’s a separate daemon, with
right now no documentation, but if you’re sufficiently technical, here
you go:

https://github.com/nriley/Karabiner-Elements-App-Profiles

If a profile is named after an app’s bundle identifier, it’ll
automatically switch to the profile when you bring that app to the
front (and the first/default profile otherwise).

Obviously this would be better built into Karabiner-Elements!

根据作者的简单介绍我们可以知道该服务的用法如下:

首先 Karabinar 需要有一个默认的 Profile,没有被指定的应用都会使用这个默认的 Profile,然后用户可以自己添加其他 Profile,如果这个 Profile 和某个应用的 Bundle Identifier 一致,那么当该应用切换到前台的时候,这个 Profile 将会自动被加载。

比如,Karabinar 配置如下的 Profile

1
2
3
4
Default Profile
com.microsoft.rdc.osx.beta
com.barebones.bbedit
com.apple.Safari

那么当打开应用 com.microsoft.rdc.osx.beta 的时候,将会自动切换对应的 Profile

定位目标应用

下面的工作就是定位我们需要特定按键的应用的 Bundle Identifier。

作者给出了两种方法,在命令行工具中获取 Bundler Identifier

launch

1
2
3
4
5
6
% launch -f /Applications/Mail.app 
/Applications/Mail.app: scriptable OS X application package
type: 'APPL' creator: 'emal'
display name: Mail
architecture: Intel x86-64
bundle ID: com.apple.mail

mdls

1
2
mdls -name kMDItemCFBundleIdentifier /Applications/Mail.app 
kMDItemCFBundleIdentifier = "com.apple.mail"

常用的应用

Windows Remote Desktop (新版,图标为圆形图标)

1
2
$ mdls -name kMDItemCFBundleIdentifier /Applications/Microsoft\ Remote\ Desktop.app/
kMDItemCFBundleIdentifier = "com.microsoft.rdc.macos"

enter description here

效果

我们配置完成后会发现,默认的时候,Karabinar 的 Profile 为默认的,但是一旦切换到其他配置了独立的 Profile 的应用,Karabinar 的 Profile将会自动切换到对应的 Profile。

enter description here

其他注意点

这种更改方案还有一点不完美就是,在切换到目标的应用后,由于键位更改,某些快捷键这时候就也会变化,比如我们常用的切换应用:Command + Tab,如果我们在交换了键位的应用上,就会变为 Ctrl + Tab,所以尽可以将常用快捷键不要依赖于应用修改的键位。

比如将 Move Left a place 和 Move Right a place 由默认的 Control + Left 变为 Option + Left,这样可以统一切换 RDP 屏幕和 Mac 自己的屏幕。

增加新的 Profile

最近增加新的 Profile 发现每次增加,一旦启动了自动切换 Profile 的应用,就是把新加入的 Profile 删除掉。后来发现了解决方法:

  1. stop karabiner-elements-app-profiles:brew services stop karabiner-elements-app-profiles
  2. 增加新的 profile
  3. start karabiner-elements-app-profiles: brew services start karabiner-elements-app-profiles

这样就不会自动删除了。

keyboard 检测失效

可以通过删除已经存在的配置文件充值键盘检测

1
sudo rm /Library/Preferences/com.apple.keyboardtype.plist

然后重启系统