Have push button change its text when key is pressed

hi therem

i wonder if it is possible to change a buttons title/name/content when the user presses a key (like the alt key by example)
so the button says “skip” and if the user presses the alt key the button changes to “skip always”.
that would be really nice!

any ideas anyone?



set title of button "BUTTON_IN_QUESTION" of window of theObject to "skip always"


I don’t think setting the title is jns’ problem … the problem is probably catching modifier key events application wide. I thought a little while about this problem because I felt there has to be an easier solution but this is the only one I found:

  1. subclass NSApplication in Interface Builder and create files for the subclass (for example named MyApplication)
  2. set MyApplication as custom class for ‘File’s Owner’
  3. connect ‘File’s Owner’ with your AppleScript - which handler does not matter, no script inside of the handler is needed.
  4. set MyApplication as ‘Principal Class’ in your Xcode project → Edit active target → Properties
  5. add this handler in your AppleScript:
on optionkeyEvent(keysState)
    tell button "but" of window "win"
        if (keysState as boolean) then
            set title to "skip always"
            set title to "skip"
        end if
    end tell
end optionkeyEvent
  1. add this code in the NSApplication subclass files:

[code]/* NSApplication subclass: MyApplication.h */

#import <Cocoa/Cocoa.h>

@interface MyApplication : NSApplication{
NSAppleScript *asOptKeyDown;
NSAppleScript *asOptKeyUp;


[code]/* NSApplication subclass: MyApplication.m */

#import “MyApplication.h”

@implementation MyApplication

asOptKeyDown=[[NSAppleScript alloc] initWithSource:@“(get my script)'s optionkeyEvent(1)”];
asOptKeyUp=[[NSAppleScript alloc] initWithSource:@“(get my script)'s optionkeyEvent(0)”];
[asOptKeyDown compileAndReturnError:nil];
[asOptKeyUp compileAndReturnError:nil];

  • (void) dealloc {
    [asOptKeyDown release];
    [asOptKeyUp release];
    [super dealloc];

  • (void)sendEvent:(NSEvent *)anEvent{
    if ([anEvent type] == NSFlagsChanged) {
    [self callAsHandler:(([anEvent modifierFlags] & NSAlternateKeyMask) != 0) ];
    [super sendEvent:anEvent];

if (state) {
[asOptKeyDown executeAndReturnError:nil];
} else {
[asOptKeyUp executeAndReturnError:nil];

Done (I hope I haven’t missed a step …) Now your button’s title should change when the option key is down/up.


Hi Dominik,

I was able to get your example to work. Works great! Where can I learn more about this stuff? What books or websites did you learn this from? At the moment, I only know Applescript.

Good stuff,

Hi CarbonQuark,

I made my fist steps into Objective-C after playing some time with the new AppleScript Studio some years ago. I saw it’s limitations and so I decided to learn some Objective-C. Though I am not at all the learning-from-books-type I read (and worked through all tutorials and examples of ) ‘Learning Cocoa with Objective-C’ (O’Reilly). I already had some C-programming experience so this gave me a good base. (Note - this book might be dated - I don’t know if there is a new edition). Everything else I learned from reading the docs, studying examples from Apple and other sources on the web, reading cocoa-dev@lists.apple.com and trial&error.

here some comments for the above solution:

Since I want to catch the keyboard events not only for the current firstResonder, I subclassed NSApplescript’s method ‘sendEvent:’ since that’s the point where every Event for this application is distributed:

- (void)sendEvent:(NSEvent *)anEvent{
// override the method by writing my own ‘version’ in the subclass

if ([anEvent type] == NSFlagsChanged) {
// checking if a modifier key has changed (up or down)

[self callAsHandler:(([anEvent modifierFlags] & NSAlternateKeyMask) != 0) ];
// call the method ‘callAsHandler’ with a boolean argument depening on the current state of the Option key

[super sendEvent:anEvent];
// executing the regular stuff of the ‘sendEvent:’ method

For a better understanding of this particular subject you should read sth about handling events and the responder chain in the docs …

In my new method ‘callAsHandler:’ I execute one of two AppleScript objects that i have inited and compiled in ‘awakefromNib’:

asOptKeyUp=[[NSAppleScript alloc] initWithSource:@“(get my script)'s optionkeyEvent(0)”];
// init a new AppleScript:
// get my script - results the applescript we connected to ‘File’s Owner’ in Interface Builder
// and (…)'s optionkeyEvent(0) calls it’s handler optionkeyEvent with argument 0

[asOptKeyUp compileAndReturnError:nil];
// compile the AppleScript for later use



Thanks for the info! I’m off to study.