Check if caps lock on in terminal

Solution 1:

There is a great command line code written by Stefenk on Macscripter

Stefen's code allows you to detect for cmd, option, control, shift, and caps lock.

In his post he provides the code and a download zip of it.

You can either download it or build your own in Xcode using foundation project.

I just tested both ways in El Capitan and they both work.

The code if you want to build your own in Xcode.app is:

#import <Carbon/Carbon.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        unsigned int modifiers = GetCurrentKeyModifiers();
        if (argc == 1)
            printf("%d\n", modifiers);
        else {
            int i, result = 1;
            for (i = 1; i < argc; ++i) {
                if (0 == strcmp(argv[i], "shift"))
                    result = result && (modifiers & shiftKey);
                else if (0 == strcmp(argv[i], "option"))
                    result = result && (modifiers & optionKey);
                else if (0 == strcmp(argv[i], "cmd"))
                    result = result && (modifiers & cmdKey);
                else if (0 == strcmp(argv[i], "control"))
                    result = result && (modifiers & controlKey);
                else if (0 == strcmp(argv[i], "capslock"))
                    result = result && (modifiers & alphaLock);
            }
            printf("%d\n", result);
        }
    }
    return 0;
}

----------( update ->

If you want to compile it from Terminal.app

Put the code into a plain text file. I just used TextEdit.app and a Plain text document.

cd to the same directory as your file. Make sure that the extension is .m ( .c will be ok also)

Then Run.

gcc  -framework Carbon CheckModKeys.m -o CheckModKeys

CheckModKeys.m is your file.

CheckModKeys is the name of the app to be outputted.

<-)


To detect caps lock your command would look something like:

/Users/UserName/MyFolder/CheckModKeys  capslock

a 1 or 0 will be returned.

1 for on

0 for off


All credit goes to Stefen. I just remembered he posted this way back in`2009. And it still works today..

Solution 2:

If you have XQuartz installed and either running or don't mind it launching, you can run: xset -q

Keyboard Control:
  auto repeat:  on    key click percent:  0    LED mask:  00000000
  XKB indicators:
    00: Caps Lock:   off    01: Num Lock:    off    02: Shift Lock:  off
    03: Group 2:     off    04: Mouse Keys:  off    05: Scroll Lock: off