bdunagan

fill the void - bdunagan

19 Oct 2010
Cocoa Tip: Hot Keys without Key Equivalents

Ever wonder why Command-A on the Mac doesn’t always select all of the text? The keystroke is actually a hot key for the “Select All” menu item under Edit for Mac applications. Interface Builder presets the menu item’s key equivalent to Command-A. If the menu item isn’t there, Command-A isn’t hooked up.

Certain applications, like Google Notifier and Remind Me Later, don’t have an Edit menu, but we only need a bit of code to create a hot key like Command-A for “Select All”. (Ironically, I found an example of this code in Google Breakpad, but Google Notifier doesn’t use it.)

# Assign the NSTextField's delegate to the controller.

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command  {
	NSEvent *event = [NSApp currentEvent];
	if (([event modifierFlags] & NSDeviceIndependentModifierFlagsMask) == NSCommandKeyMask && [[event characters] isEqual:@"a"]) {
		// "Command-A" selects all the text.
		[textField selectText:nil];
	}
	return NO;
}
Previous LinkedIn Twitter GitHub Email Next