Detect Option Key down in on Idle

Hi All,

I’m Looking for some advise.

I’m using a subclass - controller by jobu found here to detect Option Key down.
One of the things I want to do with it, is Toggle a Text Field label, to indicate one of two options if the Option key is down or Up.

Wanting to only have this trigged when the key was down, and nothing clicked etc…
I realised I could put the Call method in the On Idle handler.

I have connected the Application to On Idle, but do not have an interval set.
This Actually works. ( there is only a lag on the change of about 0.03 seconds, by my eye)

But I wanted to know what if any are the problems I may run into with using the On Idle like this.
And also is there a better way to my goal.

many thanks…


on idle theObject
	(* using this linked first responder, to detect if option key is down *)
	set optionKeyDown to (call method "optionKeyDown" of class "OptionController") as boolean
	if optionKeyDown then
		tell window 1
			set the content of text field "Clear Selected" to "Clear ALL"
		end tell
		tell window 1
			set the content of text field "Clear Selected" to "Clear Selected"
		end tell
	end if
end idle


Hi Mark,

with notifications you can do this only in ObjC

Subclass the main NSView of the window and customize the flagsChanged: method.
This method will always be invoked, when the keyboard flags have changed

for example

- (void)flagsChanged:(NSEvent *)theEvent { if ([theEvent modifierFlags] & NSAlternateKeyMask) NSLog(@"option key down"); else if (!([theEvent modifierFlags] & NSAlternateKeyMask)) NSLog(@"option key up"); }

Hi Stefan,

Thank you for your reply.

I hate when people ask me this But…
Can you please take me through the steps. I am new to this pretty much, and am still finding my way.


ok, here we go:

#1 in Xcode, create a group Classes
highlight the topmost object in the sidebar (with the Xcode icon), Choose Add > New Group from the Action Menu, name it Classes

#2 in Xcode, create class files
highlight the Classes foldet, Choose Menu File > New File > Objective-C NSView subclass, name it SKView, leave “create also .h file” checked

#3 replace the contents of SKView.h with

[code]#import <Cocoa/Cocoa.h>

@interface SKView : NSView {
IBOutlet NSTextField *clearSelected;

#4 replace the contents of SKView.m with

[code]#import “SKView.h”

@implementation SKView

  • (void)flagsChanged:(NSEvent *)theEvent
    if ([theEvent modifierFlags] & NSAlternateKeyMask) {
    [clearSelected setStringValue:@“Clear All”];
    else if (!([theEvent modifierFlags] & NSAlternateKeyMask)) {
    [clearSelected setStringValue:@“Clear Selected”];

#5 connect the UI elements in Interface Builder
Open the .nib (or .xib) file, in InterfaceBuilder click into the main window (below the title bar) to enable the view options. The title of the Inspector window must be start with “View”
In Inpector window choose View Identity, type SKView into the Class text field
In Inpector window choose View Connections, drag the small circle right of the Outlet clearSelected to your text field. then you will see the connection in the Inspector window

thats all.

Save and compile

#1 is not really necessary, but it helps to organize the source files

Thanks Stefan

I got most of that done. Up to the point of dragging the connection

My main window is a panel, with a Content view. I assume you mean me to connect the sub class SKView to the content view.
But I do not see the clearSelected? outlet

EDIT Ah forget that, I just went back and re did the View Identity and hit save after typing the Class.
I now see the clearSelected? outlet.

Have you saved the .m and .h files in Xcode?
The outlets update when the files are saved.

The event method in the subclass will invoked when the user touches any modifier key.
The outlet (text field) will be updated automatically depending on the state of the option key

Hi Stefan,

That works as expected.:smiley:
Thank you so much, I may have some more questions on it later if thats ok…

Thanks again

sure :slight_smile: