Re: Intercepting Some Keyboard Events for NSTextView
Re: Intercepting Some Keyboard Events for NSTextView
- Subject: Re: Intercepting Some Keyboard Events for NSTextView
- From: John Nairn <email@hidden>
- Date: Mon, 15 Jul 2002 09:12:17 -0600
The delegate method is some help and I was able to quickly find out what
commands are sent by using
- (BOOL)textView:(NSTextView *)aTextView
doCommandBySelector:(SEL)aSelector
{
NSLog(@"%@",NSStringFromSelector(aSelector));
return NO;
}
But, this method and a similar approach using
- (BOOL)textView:(NSTextView *)aTextView
shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString
*)replacementString;
both have the same problem. They both consider "Return" and "Enter" to
be the same thing which is to insert a new line. I was trying to
intercept keyboard events before the NSTextView processes them so I can
pick out Return and Enter as separate events. For now, just
distinguishing "Return" and "Enter" will solve my problem, but it would
nice to know a general way to intercept keyboard events before the first
responder for future needs that might want to trap more. For example,
the doCommandBySelector approach does not even see ordinary key presses.
The custom classes I tried (following examples in books) relied on the
window being the first responder which seemed to disrupt Cocoa's
automatic handling of responders with features like focus rings,
selections coloring, etc.
On Sunday, July 14, 2002, at 05:28 PM, Jake MacMullin wrote:
You do not need to create a subclass at all. You can intercept any key
press that you want by using a delegate method in a delegate for
whatever NSTextView the user is typing in.
For example, I have done this to handle the 'tab' key in a special way.
The method that you want to look at is the
textViewDoCommandBySelector() method - you should find the
documentation for this method in the 'Methods implemented by delegate'
section of the NSTextView docs. This method takes two arguments: the
textview and an NSSelector - which contains information about the key
press. In my example where I want to intercept a tab, my code looks
something like this (it is in Java - but the same principle applies in
Objective-C):
public boolean textViewDoCommandBySelector(NSTextView aTextView,
NSSelector aSelector) {
if(aSelector.name().equals("insertTab:")) {
// the 'tab' key was pressed, so do some stuff
}
}
I hope this helps,
Regards,
Jake
On Sunday, 14, 2002, at 04:01PM, John Nairn <email@hidden>
wrote:
I read about key board events in all books and the brief Apple
documentation, but I can not handle what I want and it should be easy.
My app has several NSTextViews and NSTextFields. I want to intercept
keyboard events for special characters is some of them, but what I have
tried does not work. For example:
1. I made a custom NSWindow class. It only get keyboard events when it
is the first responder, but I need to select first responders by
clicking around fields, selecting with mouse, etc.
2. I made a custom subclass to NSScrollView containing an NSTextView,
but apparently the keyboard event goes directly to the NSTextView.
3. I tried to make a cusom class for the NSTextView (which sounds like
the best way to me), but Interface Builder says that custom class is
not
application for an NSTextView.
The only thing I can think of is to us a custom NSWindow class as
always
being the first responder, but then it seems like there will be a lot
of
work handling flow of key board events to the right objects and then
how
to control focus appearance, selections, and much more?
-----------------------
John Nairn
email@hidden
http://www.mse.utah.edu/~nairn
-----------------------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
-----------------------
John Nairn
email@hidden
http://www.geditcom.com
-----------------------
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.