Benches macOS

3

Caps Lock

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

My script needs to see if caps lock is on or off

Solution can be plain AppleScript thing or unix thing.

Thanks

3 replies

Sign in or join Beachball Help to reply.

  1. 0

    Budgie

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Cande

    this could help

    http://www.macosxhints.com/article.php?story=20080202094656148

    Budgie

    Message was edited by: Budgie

  2. 0

    red_menace

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    For a command line tool, there is also this one:

    http://macscripter.net/viewtopic.php?pid=114479#p114479

  3. 0

    Hiroto

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello

    You can build a simple command line programme to get current key modifiers' hardware state.

    1) Save C source below as plain text file named, .e.g, GetCurrentKeyModifiers.c, on desktop.


    #include <Carbon/Carbon.h>
    int main (int argc, const char * argv[]) {
    unsigned int m = GetCurrentKeyModifiers();
    printf("%u ", m);
    return 0;
    }

    2) In Terminal.app, change current directory to desktop and use gcc to compile it as follows.

    (You need Developer Tools installed to use gcc.).


    cd ~/Desktop
    gcc -framework Carbon -o GetCurrentKeyModifiers GetCurrentKeyModifiers.c

    3) Test the yielded command in Terminal.app.


    ./GetCurrentKeyModifiers

    If it prints 1024 (or any number N where N & 0x0400 = 1) when CAPS LOCK is on, you're done.

    ---

    Now in AppleScript you'd write as follows to detect caps lock.


    --SCRIPT
    do shell script ("~/Desktop/GetCurrentKeyModifiers")
    set n to result as number
    set _capslocked to n div 1024 mod 2 = 1
    --END OF SCRIPT

    cf.

    http://developer.apple.com/documentation/Carbon/Reference/Event_Manager/

    http://developer.apple.com/documentation/Carbon/Reference/EventManager/EventManager.pdf


    Event Modifier Constants
    enum {
    activeFlagBit =0,
    btnStateBit =7,
    cmdKeyBit =8,
    shiftKeyBit =9,
    alphaLockBit =10,
    optionKeyBit =11,
    controlKeyBit =12,
    rightShiftKeyBit =13,
    rightOptionKeyBit =14,
    rightControlKeyBit =15
    };
    typedef UInt16 EventModifiers;

    Good luck,

    H