从 Mac 上的命令行启用/禁用 Fn 键

发布于 2024-09-13 10:17:03 字数 225 浏览 7 评论 0 原文

我几乎从不使用 MacBook Pro 上的功能键。我主要只是将它们用于音量、亮度等。现在我已经开始玩《星际争霸 2》了,我想在不按下 fn 键的情况下使用它们。

我想编写一个小 shell 脚本来翻转“使用所有 F1、F2 等键作为标准功能键”复选框。我想我可以使用默认命令来更改它,但我不确定要使用什么值。这样我就不必每次想玩时都更改首选项。我只需运行脚本即可切换按键,甚至启动游戏。

有什么想法吗?

I hardly ever use the function keys on my macbook pro. I mostly just use them for volume, brightness, etc. Now that I've started playing Starcraft 2 a bunch, I want to use them without having to press the fn key down.

I want to write a little shell script that will flip the "Use all F1, F2, etc keys as standard function keys" check box. I was thinking I could use the defaults command to change it but I wasn't sure what values to use. This way I don't have to change the the preferences every time I want to play. I can just run the script that'll switch the keys and even launch the game.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不…忘初心 2024-09-20 10:17:03

一个可以解决这个问题的 AppleScript —— 取自 http://scriptbuilders.net/files/fn1。 1.html,略有修改

--Check if GUI Scripting is Enabled
tell application "System Events"
    if not UI elements enabled then
        set UI elements enabled to true
    end if
end tell

--Enable/Disable "Use all F1, F2, etc. keys as standard function keys" option in Keyboard & Mouse Preference pane and close System Preferences
tell application "System Events"
    tell application "System Preferences"
        reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
    end tell
    click checkbox 1 of tab group 1 of window 1 of application process "System Preferences"
end tell
if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if

在MacOS 10.6.4上测试

An AppleScript that should do the trick -- taken from http://scriptbuilders.net/files/fn1.1.html, with slight modifications

--Check if GUI Scripting is Enabled
tell application "System Events"
    if not UI elements enabled then
        set UI elements enabled to true
    end if
end tell

--Enable/Disable "Use all F1, F2, etc. keys as standard function keys" option in Keyboard & Mouse Preference pane and close System Preferences
tell application "System Events"
    tell application "System Preferences"
        reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
    end tell
    click checkbox 1 of tab group 1 of window 1 of application process "System Preferences"
end tell
if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if

Tested on MacOS 10.6.4

情痴 2024-09-20 10:17:03

该命令是 defaults write -g com.apple.keyboard.fnState,尽管我过去在更改它时遇到了问题。我最终只使用了 AppleScript。尝试一下。

defaults write -g com.apple.keyboard.fnState -boolean true

编辑
详细说明,我遇到的问题是实际值已更改,但它不会主动更改系统偏好设置中的设置,也不会切换 fnState,因为该文件是仅在启动/登录等时读取。此外,对另一个任务打开的配置文件进行更改听起来是损坏文件的好方法。

The command is defaults write -g com.apple.keyboard.fnState, although I've had problems in the past changing it. I ended up just using an AppleScript. Give it a try.

defaults write -g com.apple.keyboard.fnState -boolean true

Edit
To elaborate, the problems I've had is that the actual value is changed, but it doesn't actively change the setting in System Preferences nor does the fnState toggle, because the file is only read at boot/login etc. Also, making changes to a config file that's opened by another task sounds like a good way to corrupt the file.

时光暖心i 2024-09-20 10:17:03

您可以安装很棒的 Karabiner-Elements

在系统偏好设置下->键盘首选项,确保选中“使用所有 F1、F2 等键作为标准功能键”作为必要条件。

  • 打开 KeyRemap4MacBook 首选项。
  • 导航至“传递模式”选项。
  • 检查“更改 Fn+Escape 以切换“直通模式””
  • 打开“更改 F1..F19 键”并检查“Macbook Pro”或“Macbook Air”选项,选择正确的 Mac 类型。

You can install the awsome Karabiner-Elements.

Under System Preferences-> Keyboard preferences, make sure "Use all F1, F2, etc. keys as standard function keys" is checked as a perquisites.

  • Open KeyRemap4MacBook preferences.
  • Navigate to "Pass Through Mode" option.
  • Check the 'Change Fn+Escape to toggle "Pass Through Mode"'
  • Open "Change F1..F19 Key" and check the "Macbook Pro" or "Macbook Air" option choosing your correct mac type.
作业与我同在 2024-09-20 10:17:03

对于任何其他试图让这项工作发挥作用的人来说——我终于找到了可行的解决方案。测试环境:MacOS Big Sur,11.4,2021 年 6 月。

代码基于此处:
https://github.com/MrSimonC/Toggle-Mac-Function-Keys

但为了简洁起见,这里是苹果脚本文件的内容:

-- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
-- Tested on MacOS Big Sur (11.4) June 2021
-- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys

tell application "System Preferences"
    set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    if UI elements enabled then
        tell application process "System Preferences"
            repeat until exists tab group 1 of window "Keyboard"
                delay 0.5
            end repeat
            click radio button "Keyboard" of tab group 1 of window "Keyboard"
            click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
        end tell
        tell application "System Preferences" to quit
    else
        -- GUI scripting not enabled.  Display an alert
        tell application "System Preferences"
            activate
            set current pane to pane "com.apple.preference.security"
            display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
        end tell
    end if
end tell

希望有人发现它有用!

For anyone else trying to make this work - I've finally gotten my solution to work. Tested with: MacOS Big Sur, 11.4, June 2021.

The code is based here:
https://github.com/MrSimonC/Toggle-Mac-Function-Keys

but for brevity, here is the contents of the apple script file:

-- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
-- Tested on MacOS Big Sur (11.4) June 2021
-- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys

tell application "System Preferences"
    set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    if UI elements enabled then
        tell application process "System Preferences"
            repeat until exists tab group 1 of window "Keyboard"
                delay 0.5
            end repeat
            click radio button "Keyboard" of tab group 1 of window "Keyboard"
            click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
        end tell
        tell application "System Preferences" to quit
    else
        -- GUI scripting not enabled.  Display an alert
        tell application "System Preferences"
            activate
            set current pane to pane "com.apple.preference.security"
            display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
        end tell
    end if
end tell

Hope someone finds it useful!

隔纱相望 2024-09-20 10:17:03

感谢@simonc的回答,可以轻松地将它与光线投射一起使用。

如果有人想要通过终端运行该功能:

  1. 将以下脚本保存为 sh 文件,例如:toggle-fn.sh

    osascript < 辅助功能下激活此应用程序,以便它可以访问所需的设置。”
                结束告诉
            结束如果
        结束告诉
    结束如果
    
    排爆
    
  2. 使用 sh toggle-fn.sh 在终端上运行

或者如果有人使用 raycast 并希望通过其命令功能拥有此功能:

  1. 将以下脚本保存为 sh 文件,例如:toggle-fn.sh
    #!/bin/bash

# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Toggle Fn
# @raycast.mode silent

# Optional parameters:
# @raycast.icon

Thanks to @simonc's answer, easily using it with raycast.

If anybody wants to have the functionality to be run through terminal:

  1. save below script as an sh file, eg: toggle-fn.sh

    osascript <<EOD
    
    -- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
    -- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
    -- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys
    
    set osver to system version of (system info)
    
    if osver ≥ 13.0 then
        open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"
    
        tell application "System Events" to tell process "System Settings"
            # example window title: "Keyboard – 86%", so "begins with" is needed
            repeat until window begins with "Keyboard" exists
            end repeat
    
            # wait until Keyboard window is the main window of the application and is accessible
            repeat until exists of (1st window whose value of attribute "AXMain" is true)
            end repeat
    
            # wait until the group is displayed (needed else fails on Apple M2 Pro)
            repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
            end repeat
    
            # "Keyboard Shortcuts..." Button
            set keyboardButton to 1
            # in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
            if osver ≥ 14.0 then
                set keyboardButton to 3
            end if
            click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
    
            repeat until sheet 1 of window 1 exists
            end repeat
    
            # Click Function Keys
            keystroke "f"
    
            repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
            end repeat
    
            click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
    
            # "Done" Button - Close the sheet so the application can quit
            click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
    
            # Attempting to check the sheet at a certain point while closing will throw an error
            # In that case, the outer repeat will try again
            repeat
                try
                    repeat while sheet 1 of window 1 exists
                    end repeat
                    exit repeat
                end try
            end repeat
        end tell
    
        tell application "System Settings" to quit
    else if osver < 13.0 then
        -- Below for MacOS Monterey and below
        tell application "System Settings"
            set current pane to pane "com.apple.preference.keyboard"
        end tell
    
        tell application "System Events"
            if UI elements enabled then
                tell application process "System Preferences"
                    repeat until exists tab group 1 of window "Keyboard"
                        delay 0.5
                    end repeat
                    click radio button "Keyboard" of tab group 1 of window "Keyboard"
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard"
                    end try
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
                    end try
                end tell
                tell application "System Settings" to quit
            else
                -- GUI scripting not enabled.  Display an alert
                tell application "System Settings"
                    activate
                    set current pane to pane "com.apple.preference.security"
                    display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
                end tell
            end if
        end tell
    end if
    
    EOD
    
  2. run on terminal using sh toggle-fn.sh

Or if anyone is using raycast and want to have this functionality through its command functionality:

  1. save below script as an sh file, eg: toggle-fn.sh
    #!/bin/bash

    # Required parameters:
    # @raycast.schemaVersion 1
    # @raycast.title Toggle Fn
    # @raycast.mode silent

    # Optional parameters:
    # @raycast.icon ????

    # Documentation:
    # @raycast.description Toggle Fn Keys
    # @raycast.author smthng
    # @raycast.authorURL https://github.com/smthng
    # @raycast.packageName System

    osascript <<EOD
  
    -- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
    -- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
    -- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys
    
    set osver to system version of (system info)
    
    if osver ≥ 13.0 then
        open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"
        
        tell application "System Events" to tell process "System Settings"
            # example window title: "Keyboard – 86%", so "begins with" is needed
            repeat until window begins with "Keyboard" exists
            end repeat
            
            # wait until Keyboard window is the main window of the application and is accessible
            repeat until exists of (1st window whose value of attribute "AXMain" is true)
            end repeat
            
            # wait until the group is displayed (needed else fails on Apple M2 Pro)
            repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
            end repeat
            
            # "Keyboard Shortcuts..." Button
            set keyboardButton to 1
            # in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
            if osver ≥ 14.0 then
                set keyboardButton to 3
            end if
            click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
            
            repeat until sheet 1 of window 1 exists
            end repeat
            
            # Click Function Keys
            keystroke "f"
            
            repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
            end repeat
            
            click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
            
            # "Done" Button - Close the sheet so the application can quit
            click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1
            
            # Attempting to check the sheet at a certain point while closing will throw an error
            # In that case, the outer repeat will try again
            repeat
                try
                    repeat while sheet 1 of window 1 exists
                    end repeat
                    exit repeat
                end try
            end repeat
        end tell
        
        tell application "System Settings" to quit
    else if osver < 13.0 then
        -- Below for MacOS Monterey and below
        tell application "System Settings"
            set current pane to pane "com.apple.preference.keyboard"
        end tell
        
        tell application "System Events"
            if UI elements enabled then
                tell application process "System Preferences"
                    repeat until exists tab group 1 of window "Keyboard"
                        delay 0.5
                    end repeat
                    click radio button "Keyboard" of tab group 1 of window "Keyboard"
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys on external keyboards" of tab group 1 of window "Keyboard"
                    end try
                    try
                        click checkbox "Use F1, F2, etc. keys as standard function keys" of tab group 1 of window "Keyboard"
                    end try
                end tell
                tell application "System Settings" to quit
            else
                -- GUI scripting not enabled.  Display an alert
                tell application "System Settings"
                    activate
                    set current pane to pane "com.apple.preference.security"
                    display dialog "UI element scripting is not enabled. Please activate this app under Privacy -> Accessibility so it can access the settings it needs."
                end tell
            end if
        end tell
    end if

    EOD
  1. import it into raycast as a script command
  2. run it using the name "toggle fn" on raycast

note: for raycast to be able to run the shell command that requires accessibility permissions, you have to first give access to raycast on accessibility through system settings > privacy and security > accessibility

单身情人 2024-09-20 10:17:03

以下是 @Beytan 脚本的变体(仅适用于 MacOS Sonoma!),这样您可以提供一个命令行参数来以绝对方式确定您是否希望 F1..F12 键打开或关闭,而不管它们当前的状态如何状态,即它将在单击之前检查状态。
将以下内容保存为 setfn.sh 并运行 chmod +x setfn.sh 然后您可以执行 setfn.sh on 来获得 F1.. F12 可以激活 dosbox 应用程序等,或者 setfn.sh off 可以使用 MacOS 默认值,即音量、亮度等。

#!/bin/bash

action=$1

osascript <<EOF

    -- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
    -- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
    -- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys

    set osver to system version of (system info)

    if osver ≥ 13.0 then
        open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"


        tell application "System Events" to tell process "System Settings"
            # example window title: "Keyboard – 86%", so "begins with" is needed
            repeat until window begins with "Keyboard" exists
            end repeat

            # wait until Keyboard window is the main window of the application and is accessible
            repeat until exists of (1st window whose value of attribute "AXMain" is true)
            end repeat

            # wait until the group is displayed (needed else fails on Apple M2 Pro)
            repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
            end repeat

            # "Keyboard Shortcuts..." Button
            set keyboardButton to 1
            # in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
            if osver ≥ 14.0 then
                set keyboardButton to 3
            end if
            click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1

            repeat until sheet 1 of window 1 exists
            end repeat

            # Click Function Keys
            keystroke "f"

            repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
            end repeat


            set theCheckbox to checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1

            set checkBoxValue to value of theCheckbox

            if "$action" is "off" and checkBoxValue is 1 then
                click theCheckbox
            else if "$action" is "on" and checkBoxValue is 0 then
                click theCheckbox
            end if


         ##   click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1

            # "Done" Button - Close the sheet so the application can quit
            click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1

            # Attempting to check the sheet at a certain point while closing will throw an error
            # In that case, the outer repeat will try again
            repeat
                try
                    repeat while sheet 1 of window 1 exists
                    end repeat
                    exit repeat
                end try
            end repeat
        end tell
        tell application "System Settings" to quit

   end if
EOF 

the following is a variation of @Beytan's script (for MacOS Sonoma only!) such that you can provide a command-line argument to determine in an absolute manner if you want the F1..F12 keys to be on or off regardless of their current status i.e. it will check the status prior to clicking.
save the following as setfn.sh and run chmod +x setfn.sh then you can execute setfn.sh on to have the F1..F12 be activated for dosbox applications etc or setfn.sh off to have the MacOS default which is volume, brightness, etc.

#!/bin/bash

action=$1

osascript <<EOF

    -- Apple Script (i.e. Use in Apple's Script Editor Application) to Toggle Function Keys / Media keys on/off
    -- Tested on MacOS Monterey (12.6.2) July 2023, MacOS Ventura (13.4.1) July 2023, MacOS Sonoma Preview (14.0) July 2023
    -- Project Path: https://github.com/MrSimonC/Toggle-Mac-Function-Keys

    set osver to system version of (system info)

    if osver ≥ 13.0 then
        open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"


        tell application "System Events" to tell process "System Settings"
            # example window title: "Keyboard – 86%", so "begins with" is needed
            repeat until window begins with "Keyboard" exists
            end repeat

            # wait until Keyboard window is the main window of the application and is accessible
            repeat until exists of (1st window whose value of attribute "AXMain" is true)
            end repeat

            # wait until the group is displayed (needed else fails on Apple M2 Pro)
            repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
            end repeat

            # "Keyboard Shortcuts..." Button
            set keyboardButton to 1
            # in MacOS 14 Sonoma, button 1 & 2 are assigned to keyboard brightness
            if osver ≥ 14.0 then
                set keyboardButton to 3
            end if
            click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1

            repeat until sheet 1 of window 1 exists
            end repeat

            # Click Function Keys
            keystroke "f"

            repeat until checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1 exists
            end repeat


            set theCheckbox to checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1

            set checkBoxValue to value of theCheckbox

            if "$action" is "off" and checkBoxValue is 1 then
                click theCheckbox
            else if "$action" is "on" and checkBoxValue is 0 then
                click theCheckbox
            end if


         ##   click checkbox "Use F1, F2, etc. keys as standard function keys" of group 1 of scroll area 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1

            # "Done" Button - Close the sheet so the application can quit
            click button 1 of group 2 of splitter group 1 of group 1 of sheet 1 of window 1

            # Attempting to check the sheet at a certain point while closing will throw an error
            # In that case, the outer repeat will try again
            repeat
                try
                    repeat while sheet 1 of window 1 exists
                    end repeat
                    exit repeat
                end try
            end repeat
        end tell
        tell application "System Settings" to quit

   end if
EOF 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文