How to get key events correctly
How to get key events correctly
- Subject: How to get key events correctly
- From: Timothy Mowlem <email@hidden>
- Date: Fri, 23 Mar 2007 23:55:57 +0000
Hello,
I am fairly new to Cocoa and I have a problem with not being able to
get a keyDown event handler called in a simple single window Cocoa
app I have written to play with QTKit. The app has a single window
containing a QTMovieView, a TextField and two buttons.
It uses a custom class QTKTest which extends NSResponder and
contains action methods. It implements:
(1) (void) keyDown: (NSEvent *) theEvent
(2) acceptsFirstResponder to return YES
Also it is connected to the Window's initialFirstResponder outlet
I thought that it should now be sent key events which occur in the
window since it is the first responder (I am assuming that the fact
that the outlet in NSWindow which is called initialFirstResponder
should be connected to my class and that this is what makes it the
first responder). My event handler is:
- (void) keyDown: (NSEvent *) theEvent
{
NSLog(@"### keyDown");
unichar ch = [[theEvent characters] characterAtIndex: 0];
if (ch == 'g')
{
[movie play];
}
else if (ch == 's')
{
[movie stop];
}
else
{
[super keyDown: theEvent];
}
}
I see no log messages and just get a beep when I press any key (I
have tested about 6 keys including g and s).
The Cocoa book I am reading (Cocoa Programming by Anguish, Buck and
Yacktman) always seems to use a custom NSView subclass rather than a
subclass of NSResponder directly. But since NSResponder is the class
which abstracts event handling it seems that subclassing NSResponder
should be sufficient.
Does anyone have an idea what I am doing wrong? Do I have to subclass
NSView in order to get sent events by the window?
Thanks,
Tim Mowlem
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden