0

I'm trying to trap the CMD+ and CMD- keyboard actions in my NSView - to increase and decrease the font size of some custom text. For various reasons, I can't simply use an NSTextView. Does anyone know the correct way to handle this?

As I understand it, they are key equivalents for menu items. I can 'enable' the menu items by implementing changeFont: and I can trap the keypress by implementing performKeyEquivalent: but this seems a little bit of a hack. Also when changeFont does get called, it's not clear how I interpret the fact that it was called for CMD+/- since it simply sends me an NSFontManager. Makes me wonder if I'm handling the wrong message?

Thanks in advance.

Scotty
  • 2,019
  • 1
  • 20
  • 31
  • Have you looked at subclassing `[NSView keyDown:]`? – trojanfoe Jun 05 '13 at 10:15
  • keyDown doesn't get called for CMD+/- because it's a key equivalent (I believe) It also doesn't solve the problem of how to handle the case where the equivalent menu option Text->Font->Bigger/Smaller is selected directly. Thanks – Scotty Jun 05 '13 at 11:01
  • NSFontManager's `-modifyFont:` message says that when you receive the `-changeFont:` action message, you call the NSFontManager's `-convertFont:` method with your current font. It will return a font modified based on the users choice. – Gerd K Jun 05 '13 at 15:25

1 Answers1

1

Gerd (see comments above) got me sorted. In my changeFont handler I create a temp font of size 10 then pass it back to the font manager and check the size of the returned font. If it's bigger then CMD+ was pressed, if it was smaller then CMD- was pressed.

- (void)changeFont:(id)sender
{
    NSFontManager* fm = sender;

    NSFont* fntBefore = [NSFont systemFontOfSize:10];
    NSFont* fntAfter = [fm convertFont:fntBefore];

    CGFloat delta = fntAfter.pointSize - fntBefore.pointSize;
}
Scotty
  • 2,019
  • 1
  • 20
  • 31