There is a good solution here: http://lists.apple.com/archives/applescript-users/2009/Sep/msg00374.html
Take the following code:
#import <Carbon/Carbon.h>
int main (int argc, const char * argv[]) {
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;
}
Paste it into a file called for example. keys.m.
Then create a command line utility as follows:
$ gcc keys.m -framework Carbon -o keys
Put the executable keyssomewhere in your path, for example. /usr/local/binor even in the same directory as your bash script, then you can name it, for example. keys optionand check the returned string for "0" or "1".
source
share