How to catch command down?

I would like to unhide some control views in a utility panel when the command key is pressed (without any character key associated with the command key).

I thought the flagsChanged method was a good candidate for this but I don’t know how to make it work.

A clue?

You’ll probably need to override the -keyDown: method and check what key’s pressed. Probably on the window.

Hi Shane,

keyDown seems to triggers only if a character key is associated (e.g. command+C).

I’ve sub-classed NSPanel.
In the .m file I added:

 - (void)flagsChanged:(NSEvent *)theEvent
    flags = (theEvent.modifierFlags & NSEventModifierFlagCommand) != 0;
    NSLog(@"Flag changed %d", flags);
    [super flagsChanged:theEvent]; -- not sure this is necessary

I can see in the Debug Area of Xcode that the code is working:

My problem is that I don’t know how to get those results in the AppDelegate script.

You just have that method call one in your app delegate:

[[NSApp delegate] commandPressed]
1 Like

Sorry Shane, I don’t get it.

Where I’m supposed to put this line of code?
I presume it’s not in the AppDelegate.applescript…

Is it in the .h or .m file?

I’m totally lost!

In your subclassed panel:

 - (void)flagsChanged:(NSEvent *)theEvent
    flags = (theEvent.modifierFlags & NSEventModifierFlagCommand) != 0;
    if (flags) {
       [[NSApp delegate] commandPressed];
    [super flagsChanged:theEvent]; -- this is necessary

In your app delegate;

on commandPressed()
-- whatever

It’s throwing an error:

Add an informal protocol that defines the method. It should go just above the @implementation part:

@interface NSObject (ASCompatability)

- (void)commandPressed;


Not working.

I’ve made a new (testing) app and reconfigured everything, just in case…
The selector remains unknown.

Do we have to declare it in the header file?

It looks like you’ll have to convince the compiler that your app delegate is an NSObject:

- (void)flagsChanged:(NSEvent *)theEvent
    if (theEvent.modifierFlags & NSEventModifierFlagCommand) {
        [(NSObject *)[NSApp delegate] commandPressed];
   [super flagsChanged:theEvent];

Yeeeesss! This is it!

After hours and hours passed on this, I can’t say how much I’m grateful to you!