SOLVED: Using the tab key to cycle through custom NSViews?
SOLVED: Using the tab key to cycle through custom NSViews?
- Subject: SOLVED: Using the tab key to cycle through custom NSViews?
- From: David Hoerl <email@hidden>
- Date: Mon, 10 Sep 2007 11:32:48 -0400
Sending this for the archives (hope I can save some other poor soul
some grief).
---
I cannot say that this is the best way to solve the problem of using
tab to cycle through custom NSViews, but I did finally get it to work.
The key was this method in NSWindow:
NSWindow: selectNextKeyView:
This action method searches for a candidate key view and, if it finds
one, invokes makeFirstResponder: to establish it as the first
responder.
- (void)selectNextKeyView:(id)sender
Discussion
The candidate is one of the following (searched for in this order):
* The current first responder's next valid key view, as returned by
NSView's nextValidKeyView method
* The object designated as the receiver's initial first responder
(using setInitialFirstResponder:) if it returns YES to an
acceptsFirstResponder message
* Otherwise, the initial first responder's next valid key view,
which may end up being nil.
At first, I tried to implement performKeyEquivalent: in my view,
looking for the tab key. What I observe is that NSWindow passes the
tab key down the responder chain starting with the
initialfirstResponder (not the firstResponder!). When I switched to
keyDown:, then it went right to the first responder. [Thus, you can
use either method subject to the differences noted.]
Thus, the full set of custom NSView methods I used for cycling is below:
- (BOOL)acceptsFirstResponder
{
return YES;
}
//- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
- (BOOL)keyDown:(NSEvent *)theEvent // canBecomeKeyView
{
NSString *characters;
// if using performKeyEquivalent: enable below
// if([[self window] firstResponder] != self) return NO;
// get the pressed key
characters = [theEvent charactersIgnoringModifiers];
if ([characters isEqual:@"\t"]) {
[[self window] selectNextKeyView:self];
return YES;
} else {
return NO;
}
}
- (NSView *)nextValidKeyView
{
NSView *a;
return [super [super nextKeyView]];
}
Implementing shift-tab is an exercise left for the reader.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden