MacOS – AppleScript to check if fn key is depressed

applescriptkeyboardmacos

How can I return 1 when the fn is depressed and 0 otherwise?

I found this page that claims it has a solution but it relies on an external shell script file (I'd rather have a pure AppleScript solution if that's possible) which I can't download because it redirects me through some weird dutch scam sites.

The aim of this is actually to have a way of checking for function key + fn key combinations in ControllerMate, which doesn't seem to have a way to check if fn is pressed or not.

Best Answer

Straight from the post by Oscar...

property vers : "1.0"
my isModifierKeyPressed("function") // the only addition to his raw script

on isModifierKeyPressed(checkKey)
    set modiferKeysDOWN to {command_down:false, option_down:false, control_down:false, shift_down:false, caps_down:false, numlock_down:false, function_down:false}

    if checkKey = "" or checkKey = "option" or checkKey = "alt" then
        if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlternateKeyMask '") > 1 then
            set option_down of modiferKeysDOWN to true
        end if
    end if

    if checkKey = "" or checkKey = "command" then
        if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSCommandKeyMask '") > 1 then
            set command_down of modiferKeysDOWN to true
        end if
    end if

    if checkKey = "" or checkKey = "shift" then
        if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSShiftKeyMask '") > 1 then
            set shift_down of modiferKeysDOWN to true
        end if
    end if

    if checkKey = "" or checkKey = "control" then
        if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSControlKeyMask '") > 1 then
            set control_down of modiferKeysDOWN to true
        end if
    end if

    if checkKey = "" or checkKey = "caps" or checkKey = "capslock" then
        if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlphaShiftKeyMask '") > 1 then
            set caps_down of modiferKeysDOWN to true
        end if
    end if

    if checkKey = "" or checkKey = "numlock" then
        if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSNumericPadKeyMask'") > 1 then
            set numlock_down of modiferKeysDOWN to true
        end if
    end if
    --Set if any key in the numeric keypad is pressed. The numeric keypad is generally on the right side of the keyboard. This is also set if any of the arrow keys are pressed

    if checkKey = "" or checkKey = "function" or checkKey = "func" or checkKey = "fn" then
        if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSFunctionKeyMask'") > 1 then
            set function_down of modiferKeysDOWN to true
        end if
    end if
    --Set if any function key is pressed. The function keys include the F keys at the top of most keyboards (F1, F2, and so on) and the navigation keys in the center of most keyboards (Help, Forward Delete, Home, End, Page Up, Page Down, and the arrow keys)

    return modiferKeysDOWN
end isModifierKeyPressed

if Fn is down

Result:  
{command_down:false, option_down:false, control_down:false, shift_down:false, caps_down:false, numlock_down:false, function_down:true}

else

Result:  
{command_down:false, option_down:false, control_down:false, shift_down:false, caps_down:false, numlock_down:false, function_down:false}