keypress events + responder chain

  • I'd like to be able to track Escape keypresses but I can't seem to
    get any control over the responder chain. This is very frustrating.

    I have a NSTextField in a subclassed NSWindow.

    My NSWindow subclass contains a keyDown: method to see if the Escape
    button is pressed.

    I connected the initialFirstResponder of my NSWindow subclass to the
    nib's File Owner.

    When I first start the application, it responds to Escape keypresses.

    If I click inside the NSTextField, I then lose the ability to track
    Escape keypresses.

    How do I reconnect my NSWindow to the responder chain, or otherwise
    continue to be able to track Escape keypresses after clicking inside
    the NSTextField, or after clicking Tab to get outside the NSTextField?

    If I set -resignFirstResponder to NO on my NSWindow subclass, I can
    keep tracking Escape keypresses, but now I can't click inside the
    NSTextField to modify its contents.

    I also tried connecting the -nextKeyView outlet from the NSTextField
    to my subclassed NSWindow's NSView. This did not work. I could not
    Tab outside the NSTextField to regain the ability to track Escape
    keypresses.

    Does anyone know how I could fix this?

    Thanks for any advice,

    Regards,
    Alex
  • I was able to use the following NSTextView delegate method to
    override the default handling of the escape key (auto-completion)
    If someone has a better way, I am all ears.

    - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)
    aSelector {
    if (aSelector==@selector(complete:)) {
      //do something in response to escape key
      return YES;
    } else {
      return NO;
    }
    }

    I'm thinking you can get the window's field editor (see
    fieldEditor:forObject:) and set it's delegate

    On Oct 17, 2006, at 1:08 AM, Alex Reynolds wrote:

    > I'd like to be able to track Escape keypresses but I can't seem to
    > get any control over the responder chain. This is very frustrating.
    >
    > I have a NSTextField in a subclassed NSWindow.
    >
    > My NSWindow subclass contains a keyDown: method to see if the
    > Escape button is pressed.
    >
    > I connected the initialFirstResponder of my NSWindow subclass to
    > the nib's File Owner.
    >
    > When I first start the application, it responds to Escape keypresses.
    >
    > If I click inside the NSTextField, I then lose the ability to track
    > Escape keypresses.
    >
    > How do I reconnect my NSWindow to the responder chain, or otherwise
    > continue to be able to track Escape keypresses after clicking
    > inside the NSTextField, or after clicking Tab to get outside the
    > NSTextField?
    >
    > If I set -resignFirstResponder to NO on my NSWindow subclass, I can
    > keep tracking Escape keypresses, but now I can't click inside the
    > NSTextField to modify its contents.
    >
    > I also tried connecting the -nextKeyView outlet from the
    > NSTextField to my subclassed NSWindow's NSView. This did not work.
    > I could not Tab outside the NSTextField to regain the ability to
    > track Escape keypresses.
    >
    > Does anyone know how I could fix this?
    >
    > Thanks for any advice,
    >
    > Regards,
    > Alex
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<buddykurz...>
    >
    > This email sent to <buddykurz...>
  • On 17 okt 2006, at 10.08, Alex Reynolds wrote:

    > I'd like to be able to track Escape keypresses but I can't seem to
    > get any control over the responder chain. This is very frustrating.
    <snip>
    > Does anyone know how I could fix this?

    I think that the most efficient way to get to an answer for you is if
    you could describe what problem you're really trying to solve. Why do
    you need to track Escape key events?

    j o a r
  • On 17 Oct 2006, at 1:08 AM, Alex Reynolds wrote:

    > I'd like to be able to track Escape keypresses but I can't seem to
    > get any control over the responder chain. This is very frustrating.

    http://cocoadev.com/?NSResponder

    Implement _cancelKey: or cancelOperation: in your NSWindow sub-class
    or delegate, that should work.
  • Thanks everyone.

    I solved this by overriding sendEvent in my NSWindow subclass:

    - (void) sendEvent: (NSEvent *) theEvent
    {
        if (([theEvent type]==NSKeyDown) && ([theEvent keyCode] == 53))
    {
      [[NSNotificationCenter defaultCenter]
    postNotificationName:@"notifyEscapeKeyPressedFromControllerWindow"
    object:self];
    }

    [super sendEvent: theEvent];
    }

    I haven't tried cancelOperation: but that seems like one possible
    explanation why keyDown: fails.

    -Alex

    On Oct 17, 2006, at 11:36 PM, Dustin Voss wrote:

    > On 17 Oct 2006, at 1:08 AM, Alex Reynolds wrote:
    >
    >> I'd like to be able to track Escape keypresses but I can't seem to
    >> get any control over the responder chain. This is very frustrating.
    >
    > http://cocoadev.com/?NSResponder
    >
    > Implement _cancelKey: or cancelOperation: in your NSWindow sub-
    > class or delegate, that should work.
    >
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today