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


1

对于游戏(魔兽世界)我想交换cmd和alt键,因为我想将它绑定为热键修饰符。问题是你无法绑定commandkey所以我每次想要播放时都必须交换设置中的键,然后再换回。

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

Answers:


0

您可以使用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 swap命令和选项:

<?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"

3

事实上你可以从终端做到这一点。不过,这基本上是疯了。

#!/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,然后您可以注销并重新打开或打开Keyboard prefpane来应用设置。


这很棒,但是您需要将-n1指定为xargs以使其一次只能占用1个字。
杰克·凯西

1

我不确定你是否可以通过Terminal做到这一点,但是一个好的解决方法就是创建一个Automator脚本来快速为你做。最简单的方法是“看我做”块,尽管它们看起来并不完美。

更复杂的方法是使用applescript,无论是单独使用还是使用Automator脚本。但是,我不确定你将如何使用Applecript,但我相信你可以搞清楚。


我用自动机器做了。我从未使用过苹果脚本。我想,当我有时间的时候,我会调查这个。
AlexanderTheißen2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.