Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Hey, what's the code to set keyboard focus to an NSView?
I have a textField which deselects when the Enter key is hit, and I need a way for it to select the NSView to respond to key presses.
Any ideas?


Thanks,
-Gan
 
Hey, what's the code to set keyboard focus to an NSView?
I have a textField which deselects when the Enter key is hit, and I need a way for it to select the NSView to respond to key presses.
Any ideas?


Thanks,
-Gan

Examine the documentation for NSWindow.
 
I've searched without success.
Most of my code is within the NSView, even my textField code is. Now when you click enter on the textField it runs this:
Code:
[self sendMessage:[textField stringValue]];
[textField setStringValue:@""];
[textField setSelected:FALSE];
That deselects the textField but there's no setSelect command for an NSView. I've noticed that you need to click the NSView to select it, is there a way you could do that programmatically instead of having to click the NSView for it to accept key response?


-Gan
 
Try something like this
Code:
[[myView window] makeFirstResponder:myView];
 
I'm sure that'd work though this code throws an exception:
Code:
[textField setSelected:FALSE];
Which does deselect the textField but also ignores all code after. Any safe way to deselect a textField?


Thanks,
-Gan
 
Got it:
Code:
[[[[NSApplication sharedApplication] delegate] window] selectNextKeyView: [[[NSApplication sharedApplication] delegate] window]];
This causes it to switch to the next view. If there's more than one view it could get messy.


-Gan
 
I believe you can abbreviate [NSApplication sharedApplication] to NSApp (it looks a tiny bit cleaner, to me anyway).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.