Skip navigation.
 
mlRe: performKeyEquivalent on NSTextField subclass
FROM : Keith Alperin
DATE : Tue Jul 04 22:27:54 2006

Greetings Cocoa-devs!

The reason that all of the instances of my text field seem to be 
connected together is that the performKeyEquivalent method is called 
on many things when you type a key combination (it might even be 
every instance of an NSResponder, but i'm not sure).  Each receiver 
must then return YES or NO if it wants to handle this particular key 
combination.  As such, we're sort of abusing this method, but this is 
how everyone seems to capture hot keys.  In order to have more than 
one of these fields in a window, your NSTextField subclass need to be 
able to determine if it is currently focused on.  You might think 
that this could be done with becomeFirstResponder and 
resignFirstResponder, but as soon as an NSTextField becomes first 
responder, it immediately resigns first responder, giving it to the 
field editor.  However, your NSTextField subclass can call [self 
currentEditor] which will return the field editor if it is currently 
focused or nil if it is not.  Thus, your performKeyEquivalent will 
look something like this:

- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
{
   if ([self currentEditor]) {
       //handle the event
     } else {
       NSLog(@"no field editor");
   }
}

I hope that this is useful to someone.

Cheers!
Keith R. Alperin


On Jun 30, 2006, at 7:07 AM, Keith Alperin wrote:

> Greetings Cocoa-devs!
>
> I have a home grown NSTextField subclass that i use to capture hot 
> keys (meaning that it captures the keycode and modifiers and 
> displays a human readable string such as "Cmd + Ctrl + A". 
> Unfortunately, it only works when there is one single instance in 
> my window.  All subsequent instances seem somehow connected to the 
> first, such that when the second field is focused and i press some 
> keys, the first field displays the key combination. My key handling 
> mojo is done in the (BOOL)performKeyEquivalent:(NSEvent *)theEvent 
> method and works similarly to the way described here (http://
> www.cocoabuilder.com/archive/message/cocoa/2006/5/3/162709).  By 
> reading that thread, i've seen that a very respectable group has 
> released some code to do this for me, but i'd really like to 
> understand what is going on here.  I'd be very grateful to someone 
> for explaining why multiple instances of my NSTextField subclass 
> behave as if they were 1 instance.
>
> Many thanks,
> Keith R. Alperin
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (<email_removed>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlperformKeyEquivalent on NSTextField subclass Keith Alperin Jun 30, 14:07
mlRe: performKeyEquivalent on NSTextField subclass Keith Alperin Jul 4, 22:27