Key event handling theory
Key event handling theory
- Subject: Key event handling theory
- From: Brian Webster <email@hidden>
- Date: Fri, 4 Jan 2002 14:46:14 -0600
I'm trying to get a handle on what the intended design patterns
are in Cocoa when it comes to handling key down events. That
is, I'd like to know what way people think is "best" for
dispatching keypresses in custom views. A simple example would
be having a table view that allows the user to press delete to
delete the selected rows, or other additions like that.
The most straightforward method is to override keyDown: in your
NSView subclass and have a big switch statement that tells what
it should do depending on what character is pressed. This
works, but it seems very un-Cocoa.
Then of course, there is the whole NSResponder system, which
deals with interpretKeyEvents:, doCommandBySelector:,
insertText:, and so on. It seems like this mechanism would be
the preferred way of doing things, but I can't really figure out
how to go about it in the way it's intended to be used.
The way that seems along the right tracks is to override the
keyDown: method in your view and then pass the event along to
interpretKeyEvents:. You would then just have to bind whatever
keys you want to the appropriate selectors and they will just
get called automatically. However, I can't figure out how to go
about the binding process, since it seems to me that key binding
affects the whole app, and not just one view class or instance.
It also seems like most of the binding stuff is geared towards
text views specifically, so I don't know if it's meant to be
used for non-text views.
I guess I just feel like I'm missing something, so if anyone has
an idea of what I've been rambling on about, please let me
know. :)
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster