Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Caps Lock

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

Solution can be plain AppleScript thing or unix thing.

Thanks

MacBook Pro, Mac OS X (10.6.1)

Posted on May 7, 2010 1:04 AM

Reply
3 replies

May 9, 2010 11:12 PM in response to Cande

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

Caps Lock

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.