Skip navigation.
 
mlRe: intercepting keydowns
FROM : Roland Silver
DATE : Sun Apr 03 02:36:03 2005

Hi Satoshi,
Thanks for your email to me and the cocoa-dev list responding to my
question about intercepting keydowns.

I followed your advice in this way:

I started an Xcode Cocoa project called EmulatorExperiment.03, and
started up Interface Builder by double-clicking MainMenu.nib. I dragged
an NSTextView into the window, created a subclass MyTextView of
NSTextView, Instantiated it, created subfiles for it, and saved the
MainMenu.nib file.

Back in Xcode, I moved MyTextView.[hm] to the Classes group, and added
a keyDown override (whose declaration I got from the documentation for
NSResponder), to myTextView.m:

- (void)keyDown:(NSEvent *)theEvent {
   NSLog(@"keyDown:theEvent = %@", theEvent);
}

figuring that if my keyDown method got called, I would see evidence of
it in the Run Log.

I built the project, ran it, and successfully entered a few characters
into the text view -- but no evidence of the overriding keyDown being
called appeared in the Run Log.

I then followed your further advice by subclassing NSApplication as
MyApplication, creating files for it (but not instantiating it: IB
won't let me), and saving the MainMenu.nib file.

Back in Xcode, I added a sendEvent override to MyApplication.m:
- (void)sendEvent:(NSEvent *)theEvent {
   NSLog(@"sendEvent:theEvent = %@", theEvent);
}
I haven't done anything else in IB, like dragging something to
something else to establish delegation, outlets, or actions.

As before, I built the project, ran it, and successfully entered a few
characters into the text view -- but no evidence of the overriding
keyDown or sendEvent being called appeared in the Run Log:
      [Session started at 2005-04-02 17:24:18 -0700.]
      Executable “EmulatorExperiment.03” has exited with status 0.

I'm appending the files main.m, MyApplication.[hm], MyTextView.[hm],
and MainMenu.nib, hoping that you can help me to get this program to
work./* MyApplication */

#import <Cocoa/Cocoa.h>

@interface MyApplication : NSApplication
{
}
@end/* MyTextView */

#import <Cocoa/Cocoa.h>

@interface MyTextView : NSTextView
{
}
@endThanks for your advice and support,
Rollo (Roland) Silver <<email_removed>>
----------------------------------------------------------

On Apr 2, 2005, at 1:35 PM, Satoshi Matsumoto wrote:

> on 05.4.3 1:59 AM, Roland Silver at <email_removed> wrote:

>> I have a simple Cocoa application with a controller and a window with
>> an NSTextView. I want to intercept keydown events on their way to the
>> text view, process them with a function or method, and send a possibly
>> different keydown event on to the text view.
>> Nothing I try works.
>> Any suggestions?

>
> Subclass NSTextView and override following method.
>
> - (void)keyDown:(NSEvent *)theEvent
>
> And if you wish to intercept  all key events including all menu short
> cuts,
> subclass NSApplication and override following method.
>
> - (void)sendEvent:(NSEvent *)theEvent
>
> Satoshi
> -----------------------------------------------------
> Satoshi Matsumoto <<email_removed>>
> 816-5 Odake, Odawara, Kanagawa, Japan 256-0802
>
>
>

Related mailsAuthorDate
mlintercepting keydowns Roland Silver Apr 2, 18:59
mlRe: intercepting keydowns Bryan Zarnett Apr 2, 19:13
mlRe: intercepting keydowns Satoshi Matsumoto Apr 2, 22:35
mlRe: intercepting keydowns Roland Silver Apr 3, 02:36
mlRe: intercepting keydowns Roland Silver Apr 3, 02:42
mlRe: intercepting keydowns James Spencer Apr 3, 06:43
mlRe: intercepting keydowns Bryan Zarnett Apr 3, 09:38
mlRe: intercepting keydowns Bryan Zarnett Apr 3, 10:07
mlRe: intercepting keydowns Satoshi Matsumoto Apr 3, 13:54
mlRe: intercepting keydowns Douglas Davidson Apr 4, 17:43