Overriding NSTextField keyDown?

  • Hello, I've been trying to override the NSTextField keyDown method,
    but it didn't seem to work. I've searched in the archives, but got
    nowhere, all I could find is to override the text field editor. But
    how could I do that?
    This is all very confusing. All I really need to do is when the user
    push return (36) or enter (76) it will run a method goTo. and
    eventually I'll want to do the up and down arrows.

    Thanks for help,
    Mr. Gecko
  • On Jan 2, 2009, at 3:45 PM, Mr. Gecko wrote:

    > Hello, I've been trying to override the NSTextField keyDown method,
    > but it didn't seem to work. I've searched in the archives, but got
    > nowhere, all I could find is to override the text field editor. But
    > how could I do that?
    > This is all very confusing. All I really need to do is when the user
    > push return (36) or enter (76) it will run a method goTo. and
    > eventually I'll want to do the up and down arrows.

    You really don't want to do that.  What you need to do is to work with
    the frameworks rather than against them, and everything will go
    smoothly.  NSTextFields and other controls handle all of the standard
    keystrokes themselves, but of course they allow you to override their
    behavior at many points.  The first thing to do would be to try some
    of the tutorials that handle text fields, in particular using IB to
    set up the target-action mechanism.

    If that doesn't quite suit your needs, but what you're interested in
    is learning when a particular field has ended editing, there's a
    standard notification you can listen for.  If you want to customize a
    text field and give special handling to certain special keys, there's
    a delegate method -control:textView:doCommandBySelector:.

    What you definitely don't want to do is override keyDown:.  In the
    first place, it isn't the text field that gets the keyDown:, but the
    field editor; but even if you had a custom field editor, you wouldn't
    want to override keyDown:, because doing so will break most input
    methods.  Dealing with raw key strokes is too crude for the needs of
    modern text input--that's why we have all of these other override
    points.  Check the archives for my earlier messages on this topic.

    Douglas Davidson
  • I didn't know this went up, I figured out how to do this like saturday.
    - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView
    doCommandBySelector:(SEL)commandSelector {
    if ([NSStringFromSelector(commandSelector)
    isEqualToString:@"insertNewline:"]) {
      [self goToURL:self];
      return YES;
    }
    return NO;
    }

    On Jan 6, 2009, at 12:28 PM, Douglas Davidson wrote:

    >
    > On Jan 2, 2009, at 3:45 PM, Mr. Gecko wrote:
    >
    >> Hello, I've been trying to override the NSTextField keyDown method,
    >> but it didn't seem to work. I've searched in the archives, but got
    >> nowhere, all I could find is to override the text field editor. But
    >> how could I do that?
    >> This is all very confusing. All I really need to do is when the
    >> user push return (36) or enter (76) it will run a method goTo. and
    >> eventually I'll want to do the up and down arrows.
    >
    > You really don't want to do that.  What you need to do is to work
    > with the frameworks rather than against them, and everything will go
    > smoothly.  NSTextFields and other controls handle all of the
    > standard keystrokes themselves, but of course they allow you to
    > override their behavior at many points.  The first thing to do would
    > be to try some of the tutorials that handle text fields, in
    > particular using IB to set up the target-action mechanism.
    >
    > If that doesn't quite suit your needs, but what you're interested in
    > is learning when a particular field has ended editing, there's a
    > standard notification you can listen for.  If you want to customize
    > a text field and give special handling to certain special keys,
    > there's a delegate method -control:textView:doCommandBySelector:.
    >
    > What you definitely don't want to do is override keyDown:.  In the
    > first place, it isn't the text field that gets the keyDown:, but the
    > field editor; but even if you had a custom field editor, you
    > wouldn't want to override keyDown:, because doing so will break most
    > input methods.  Dealing with raw key strokes is too crude for the
    > needs of modern text input--that's why we have all of these other
    > override points.  Check the archives for my earlier messages on this
    > topic.
    >
    > Douglas Davidson
    >
  • On Jan 6, 2009, at 1:05 PM, Mr. Gecko wrote:

    > I didn't know this went up, I figured out how to do this like
    > saturday.
    > - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView
    > doCommandBySelector:(SEL)commandSelector {
    > if ([NSStringFromSelector(commandSelector)
    > isEqualToString:@"insertNewline:"]) {
    > [self goToURL:self];
    > return YES;
    > }
    > return NO;
    > }
    >

    Something like that should do the trick.  I should point out that you
    can compare the selector you receive directly to
    @selector(insertNewline:); you don't need to convert to strings.

    Douglas Davidson
  • oh yeah forgot about that.

    Thanks

    On Jan 6, 2009, at 3:10 PM, Douglas Davidson wrote:

    >
    > On Jan 6, 2009, at 1:05 PM, Mr. Gecko wrote:
    >
    >> I didn't know this went up, I figured out how to do this like
    >> saturday.
    >> - (BOOL)control:(NSControl *)control textView:(NSTextView
    >> *)textView doCommandBySelector:(SEL)commandSelector {
    >> if ([NSStringFromSelector(commandSelector)
    >> isEqualToString:@"insertNewline:"]) {
    >> [self goToURL:self];
    >> return YES;
    >> }
    >> return NO;
    >> }
    >>
    >
    > Something like that should do the trick.  I should point out that
    > you can compare the selector you receive directly to
    > @selector(insertNewline:); you don't need to convert to strings.
    >
    > Douglas Davidson
    >