通过命令行切换 OS X 上的功能键?

通过命令行切换 OS X 上的功能键?

对于一款游戏(魔兽世界),我想交换 cmd 和 alt 键,因为我想将其绑定为热键修饰符。问题是您无法绑定命令键,因此每次我想玩游戏时,我都必须在设置中交换按键,然后再交换回来。

有没有办法在命令行上执行此操作,以便我可以编写脚本?

答案1

您可以使用KeyRemap4MacBook

k=/Applications/KeyRemap4MacBook.app/Contents/Applications/KeyRemap4MacBook_cli.app/Contents/MacOS/KeyRemap4MacBook_cli
$k changed | grep -q ^remap.commandL2optionL= && mode=disable || mode=enable
$k $mode remap.commandL2optionL
$k $mode remap.commandR2optionR
$k $mode remap.optionL2commandL
$k $mode remap.optionrcommandr

您可以通过使用 private.xml 按下 shift-F1 来交换命令和选项,如下所示:

<?xml version="1.0"?>
<root>
  <item>
    <name>toggleoptcmd</name>
    <identifier>toggleoptcmd</identifier>
    <autogen>__KeyToKey__ KeyCode::F1, VK_SHIFT | ModifierFlag::NONE,
    KeyCode::VK_CONFIG_TOGGLE_swapoptcmd</autogen>
  </item>
  <item>
    <name>swapoptcmd</name>
    <identifier vk_config="true">swapoptcmd</identifier>
    <autogen>__KeyToKey__ KeyCode::OPTION_L, KeyCode::COMMAND_L</autogen>
    <autogen>__KeyToKey__ KeyCode::OPTION_R, KeyCode::COMMAND_R</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_L, KeyCode::OPTION_L</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_R, KeyCode::OPTION_R</autogen>
  </item>
</root>    

仅当 TextEdit 位于最前面时才会交换选项和命令:

<?xml version="1.0"?>
<root>
<appdef>
  <appname>TEXTEDIT</appname>
  <equal>com.apple.TextEdit</equal>
</appdef>
  <item>
    <name>swapoptcmdtextedit</name>
    <identifier>swapoptcmdtextedit</identifier>
    <only>TEXTEDIT</only>
    <autogen>__KeyToKey__ KeyCode::OPTION_L, KeyCode::COMMAND_L</autogen>
    <autogen>__KeyToKey__ KeyCode::OPTION_R, KeyCode::COMMAND_R</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_L, KeyCode::OPTION_L</autogen>
    <autogen>__KeyToKey__ KeyCode::COMMAND_R, KeyCode::OPTION_R</autogen>
  </item>
</root>    

另一个选择是使用 AppleScript:

tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell    
tell application "System Events" to tell window 1 of process "System Preferences"
    click button "Modifier Keys…" of tab group 1
    tell sheet 1
        --tell pop up button 5
        --click
        --click menu item "Apple Wireless Keyboard" of menu 1
        --end tell
        if value of pop up button 1 is "⌘ Command" then
            tell pop up button 1
                click
                click menu item "⌥ Option" of menu 1
            end tell
            tell pop up button 2
                click
                click menu item "⌘ Command" of menu 1
            end tell
        else
            tell pop up button 1
                click
                click menu item "⌘ Command" of menu 1
            end tell
            tell pop up button 2
                click
                click menu item "⌥ Option" of menu 1
            end tell
        end if
        click button "OK"
    end tell
end tell    
quit application "System Preferences"

答案2

事实上你可以从终端上完成这个操作。不过这基本上是疯狂的。

#!/bin/bash
# Remap capslock to control. Really.
#
# list of keyboards plugged in to this computer
keyboard_ids=$(ioreg -n IOHIDKeyboard -r | grep -E 'VendorID"|ProductID' | awk '{ print $4 }' | paste -s -d'-\n' -)
# check if the keyboards are already remapped
echo $keyboard_ids | xargs -I{} sh -c 'defaults -currentHost read -g "com.apple.keyboard.modifiermapping.{}-0" | grep "Dst = 2" > /dev/null'
if [[ $? -ne 0 ]]; then
  # remap the keyboards
  echo $keyboard_ids | xargs -I{} defaults -currentHost write -g "com.apple.keyboard.modifiermapping.{}-0" -array "<dict><key>HIDKeyboardModifierMappingDst</key><integer>2</integer><key>HIDKeyboardModifierMappingSrc</key><integer>0</integer></dict>"
fi

情况发生变化~/Library/Preferences/ByHost/.GlobalPreferences.<machine_identifier>.plist,然后您可以注销并重新登录或打开键盘首选项窗格来应用该设置。

答案3

我不确定你是否可以通过终端来做这件事,但一个好的解决方法是创建一个 Automator 脚本来快速为你完成这件事。最简单的方法是使用“看我怎么做”块,尽管它们看起来一点也不精致。

更复杂的方法是使用 applescript,可以单独使用,也可以与 Automator 脚本一起使用。不过,我不确定您具体如何使用 applescript,但我相信您一定能搞清楚。

相关内容