Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 11, 2013, 02:46 PM   #1
Dreamspinner
macrumors member
 
Join Date: Dec 2012
Keydown isn't firing.

OS X 10.8.2, XCode 4.5.2.

I have a textbox that I need to limit input to numeric, 3 digits (3 must be 003) between 0 and 360 inclusive. I also need to trap the Enter key.

My thought was to do the validation in a keyDown method. After a lot of searching and reading, it seems I need to have
Code:
- (BOOL)acceptsFirstResponder
{
  NSLog(@"Accepting...");
  return YES;
}

- (BOOL)becomeFirstResponder
{
  NSLog(@"becoming...");	
  return YES;
}
- (void)KeyDown:(NSEvent *)event
{
  NSLog(@"a key wuz prest");
  NSString *characters = [event charactersIgnoringModifiers];
  if([characters isEqualToString:@"v"])
  {
  }
}
This code is in my app delegate, as is all the other code that interacts with the UI.
None of those three methods fire. I think something's not wired correctly, but no idea what.

I'd also welcome any comments if catching the keys in keyDown isn't the best way to go.
Dreamspinner is offline   0 Reply With Quote
Old Jan 11, 2013, 07:03 PM   #2
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Your app delegate is near the bottom of the responder chain, so it is just not getting the keydown event: the field is simply swallowing it. You could use a custom text field subclass that overrides -keyDown: or you could subclass NSNumberFormatter and attach that to the field. Otherwise, I think you can attach a delegate to the field itself or get notifications from it.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Jan 12, 2013, 09:40 AM   #3
Dreamspinner
Thread Starter
macrumors member
 
Join Date: Dec 2012
I see the app delegate at the bottom of the chain. What I don't get is how buttons on the same view as my text box can respond and the text box doesn't.

Even if I call the acceptsFirstResponder and becomeFirstResponder, they have no effect I can see. Isn't there a way to move the app delegate up the chain?

[rant]I can't believe that this is such an uncommon a task and so esoteric.[/rant]

Of your suggestions I think a custom field/override -keydown appeals the most, second would be attaching a delegate.

Any tips there?
Dreamspinner is offline   0 Reply With Quote
Old Jan 12, 2013, 10:21 AM   #4
mfram
macrumors 6502a
 
Join Date: Jan 2010
Doesn't your method have the wrong name? You have "KeyDown". It should be "keyDown". But either way, if you are trying to limit the keystrokes that get accepted by the control, I'd bet you are going to have to make a custom control. I'm not sure how the delegate protocol works, but it would have to be earlier in the responder chain than the control or the control will consume the event before you get a change to filter it out.

Last edited by mfram; Jan 12, 2013 at 10:27 AM.
mfram is offline   0 Reply With Quote
Old Jan 12, 2013, 10:46 AM   #5
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Dreamspinner View Post
Of your suggestions I think a custom field/override -keydown appeals the most, second would be attaching a delegate.

Any tips there?
Well spotted by mfram that you didn't actually have a keyDown method at all, but a completely unrelated method named KeyDown which the OS is never going to call.

Now since your text field is only supposed to accept numbers, what should happen if I paste in some text? Overriding keyDown won't help there at all (and you have to implement cut/copy/paste and so on). It isn't actually of any interest what keys the user is pressing. What interests you is what text is in the field. So find a delegate method that is called when the user changes the text.
gnasher729 is offline   2 Reply With Quote
Old Jan 13, 2013, 07:46 AM   #6
Dreamspinner
Thread Starter
macrumors member
 
Join Date: Dec 2012
Good eye on the name. Changing it to keyDown didn't help. So it's getting eaten somewhere somehow. I just can't dope out how/where.

----------

Quote:
Originally Posted by gnasher729 View Post
Now since your text field is only supposed to accept numbers, what should happen if I paste in some text? Overriding keyDown won't help there at all (and you have to implement cut/copy/paste and so on). It isn't actually of any interest what keys the user is pressing. What interests you is what text is in the field. So find a delegate method that is called when the user changes the text.
Yes you're right about catching a keyDown being the wrong strategy. Off to look for text changing. I'd still like to know how to do the keyDown, but it can wait.
Dreamspinner is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 10:18 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC