Caps Lock Detection and Growl

Greetings, folks!

As much as I love my wireless keyboard (and can’t afford to replace it), my Logitech Control Center software has been tending to misbehave for a few years; the issue is any kind of notification as to the status of the Caps Lock key. Growl has a wonderful AppleScript capability and, AFAIK, Extra Suites is the only way to detect the status of the Caps Lock key. Thus I have written a simple, stay-open AppleScript app that really does work well . . . for a while.

After the Caps Lock key has been depressed for more than a few minutes, the response time of the script steadily increases such that I can release the Caps Lock key and the alert will keep going for many seconds. If left displaying the alert for too long, it eventually generates an error.

(I use the “Nano” Growl style with the opacity and duration both set to 0. The icon is just a single pixel so as to not be noticeable.)

Does anyone have any wisdom as to how to “smooth out” this process? Is there any other way to detect the status of the Caps Lock key?

Thank you!!

Richard Fairbanks

on idle
	set x to 0
	tell application "Extra Suites"
		if (ES keys down) contains {"caps lock"} then set x to 1
	end tell
	if x is 1 then tell application "GrowlHelperApp" to notify with name "Caps Lock Notification" title "Caps Lock" description "" application name "Caps Lock Notification" image from location "/Users/me/Scripts/Growl Alerts/Caps Lock Icon.tiff"
	return 2 -- this seems to be a good compromise for response time
end idle

Model: 2.33 GHz MBPC2D
Operating System: Mac OS X (10.4)

Hi,

in Leopard there is a alternative way, but I doubt it works in Tiger


on idle
	tell application "System Events"
		tell application "KeyboardViewerServer" to activate
		set CAPS to (get value of checkbox "⇪" of window 1 of application process "KeyboardViewerServer")
	end tell
	if CAPS is 1 then tell application "GrowlHelperApp" to notify with name "Caps Lock Notification" title "Caps Lock" description "" application name "Caps Lock Notification" image from location "/Users/me/Scripts/Growl Alerts/Caps Lock Icon.tiff"
	return 2 -- this seems to be a good compromise for response time
end idle

Greetings, Stefan!

Thanks once again for your help!

Yes, your script doesn’t run on 10.4, but I just didn’t see enough value in 10.5–I’m waiting with much anticipation for 10.6, though!

Please allow me to elaborate on the error I’m getting, as it mystifies me and I can’t find any information on it:

Can't continue «event EXSUKEYD»

Thoughts, anyone?

Thanks!

! But this works great !

Thanks for the lead Stefan!!

on run
	tell application "KeyboardViewerServer" to run
	tell application "System Events" to tell button 3 of window 1 of process "KeyboardViewerServer" to click -- use this to minimize the app into the Dock to keep it out of sight
end run

on idle
	set CAPS to 0
	tell application "KeyboardViewerServer" to activate -- this might stop the rare "NSReceiverEvaluationScriptError: 4" error
	tell application "System Events" to set CAPS to the ASCII number of ((the name of button "c" of window 1 of application process "KeyboardViewerServer") as string)
	if CAPS is 67 then tell application "GrowlHelperApp" to notify with name "Caps Lock Notification" title "Caps Lock" description "" application name "Caps Lock Notification" image from location "/Users/me/Scripts/Growl Alerts/Caps Lock Icon.tiff"
	return 3 -- this seems to be a good compromise for response time
end idle

Thats a Nice solution for the Tiger form of that script.

Although if anyone is using the script and CAPS for a toggle, you will need to check more than one key.
As it will get activated when the shift key is depressed while the script checks if the CAPS is “c” or “C”

Yes, absolutely correct, Mark: it appears that the system has been set up to trap for ASCII number 67, regardless of whether the Caps Lock key or the Shift key is pressed. The only solution appears to be to look down and see whether you are holding the shift key down!

:wink:

(I worked on it for about an hour and got lots of data, but haven’t figured out how to put it to use yet . . . )

Thus, please allow me to offer a minor update, but first, this script demonstrates the reason:

delay 1 -- necessary for those of us who can't let go of keys fast enough!
run application "KeyboardViewerServer"
tell application "System Events" to return the properties of buttons of window 1 of application process "KeyboardViewerServer"

(*
Under most conditions, this is the result for the Caps Lock key:
*)

--> {position:{169, 240}, maximum value:missing value, name:"c", size:{41, 40}, subrole:missing value, class:button, minimum value:missing value, enabled:true, selected:missing value, role:"AXButton", help:missing value, title:"c", value:missing value, entire contents:{}, description:"button", focused:false, orientation:missing value}

(*
BUT, if the option or control keys are down, then many of the keys get their names and titles stripped out. Here is the result for the Caps Lock key under those conditions:
*)

--> {position:{169, 240}, maximum value:missing value, name:" ", size:{41, 40}, subrole:missing value, class:button, minimum value:missing value, enabled:true, selected:missing value, role:"AXButton", help:missing value, title:" ", value:missing value, entire contents:{}, description:"button", focused:false, orientation:missing value}

I couldn’t figure out a good solution, so I just added the ever-faithful “try” block:

on run
	tell application "KeyboardViewerServer" to run
	tell application "System Events" to tell button 3 of window 1 of process "KeyboardViewerServer" to click -- use this to minimize the app into the Dock
end run

on idle
	set CAPS to 0
	try -- this is necessary as the control or option keys will strip out the name of many keys, incl. the Caps Lock key!
		tell application "System Events" to set CAPS to the ASCII number of ((the name of button "c" of window 1 of application process "KeyboardViewerServer") as string)
	end try
	if CAPS is 67 then tell application "GrowlHelperApp" to notify with name "Caps Lock Notification" title "Caps Lock" description "" application name "Caps Lock Notification" image from location "/Users/me/Scripts/Growl Alerts/Caps Lock Icon.tiff"
	return 2 -- this seems to be a good compromise for response time
end idle