【问题标题】:AppleScript: Script to switch Caps Lock Key to ControlAppleScript:将 Caps Lock 键切换为 Control 的脚本
【发布时间】:2013-04-20 02:34:36
【问题描述】:

我试图找到一个解决方案,因为我正在使用 Vim,并且大多数时候我需要将 Caps lock 分配给 Ctrl。虽然有时我希望 Caps Lock 正常工作。我认为 Apple Script 很棒,因为它可以分配给 LaunchBar 动作。

我将回答我最终得到的解决方案(通过 GUI 脚本),但如果有人知道方法,我会对侵入性较小的解决方案感兴趣......

【问题讨论】:

    标签: macos vim applescript


    【解决方案1】:

    这是我目前使用的脚本:

    tell application "System Preferences"
       activate
       set current pane to pane "com.apple.preference.keyboard"
    end tell
    
    tell application "System Events"
    tell application process "System Preferences"
      get properties
    
      click button "Modifier Keys…" of tab group 1 of window "Keyboard"
      tell sheet 1 of window "Keyboard"
        click pop up button 4
        set capsLockCurrentlyOn to (value of attribute "AXMenuItemMarkChar" of menu item 1 of menu 1 of pop up button 4 as string) ≠ ""
        --display dialog "Caps Lock On: " & capsLockCurrentlyOn
        if capsLockCurrentlyOn is true then
          --tell me to beep 3
          click menu item 2 of menu 1 of pop up button 4
        else
          --tell me to beep 2
          click menu item 1 of menu 1 of pop up button 4
        end if
        click button "OK"
      end tell
    end tell
    tell application "System Preferences" to quit
    end tell
    

    【讨论】:

    • 你有没有让它正常工作?我无法使(单击弹出按钮 4 的菜单 1 的菜单项 1)工作。它只是一直选择 CTRL 选项。
    • @AlexCory 我最终使用了 KeyRemapMacBook。只有我几乎不使用它,结果我什至不记得快捷方式了!嗯...
    【解决方案2】:

    您可以使用PCKeyboardHack 将大写锁定更改为 F19。然后保存为private.xml:

    <?xml version="1.0"?>
    <root>
    <item>
    <name>caps1</name>
    <identifier>caps1</identifier>
    <autogen>__KeyToKey__ KeyCode::F19, KeyCode::CAPSLOCK</autogen>
    </item>
    <item>
    <name>caps2</name>
    <identifier>caps2</identifier>
    <autogen>__KeyToKey__ KeyCode::F19, KeyCode::CONTROL_L</autogen>
    </item>
    </root>
    

    并使用此脚本来切换设置:

    k=/Applications/KeyRemap4MacBook.app/Contents/Applications/KeyRemap4MacBook_cli.app/Contents/MacOS/KeyRemap4MacBook_cli
    if $k changed | grep -q ^caps1=; then
        $k disable caps1
        $k enable caps2
    else
        $k enable caps1
        $k disable caps2
    fi
    

    不过,将另一个组合键分配给大写锁定会更容易:

    &lt;autogen&gt;__KeyOverlaidModifier__ KeyCode::CONTROL_L, ModifierFlag::FN, KeyCode::CONTROL_L, ModifierFlag::FN, KeyCode::CAPSLOCK&lt;/autogen&gt;

    这将在按键按下时触发,因此您必须在 fn 之前按下 control 才能使用 fn+control+down 等组合键:

    &lt;autogen&gt;__KeyToKey__ KeyCode::CONTROL_L, ModifierFlag::FN, KeyCode::CAPSLOCK&lt;/autogen&gt;

    【讨论】:

      【解决方案3】:

      你试过KeyRemap4MacBookPCKeyboardHack吗?

      【讨论】:

        猜你喜欢
        • 2014-04-28
        • 2012-10-23
        • 1970-01-01
        • 2010-10-24
        • 2013-03-04
        • 1970-01-01
        • 2015-02-09
        • 2011-08-14
        • 1970-01-01
        相关资源
        最近更新 更多