• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to get key events correctly
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get key events correctly


  • Subject: Re: How to get key events correctly
  • From: Doug F <email@hidden>
  • Date: Mon, 26 Mar 2007 11:27:53 -0400

Having seen no other answers, let me respond the way I solved the problem in my application.

1) Subclass NSApplication

2) Make sure your subclass is set up in the Info.plist so it is used

3) Use a method like this in your subclass:

/** Overridden sendEvent so that we can handle a few different key presses
* differently **/
- (void)sendEvent:(NSEvent *)theEvent
{
if (controller && [controller attemptToHandleSentEvent:theEvent])
// Our controller handled this
return;
// Handle it the usual way
[super sendEvent:theEvent];
}


Some notes on my method:
a) "controller" is an "app controller" class which is set up when it is unpacked from the NIB then calls a "setController" on the global NSApp object after casting to the proper type
b) The "attempt..." method returns YES if it handled it and NO if the system default should occur. That method does a lot of checks to ensure if it should handle it first (e.g., is the window the main window, is the application in front, is the state of the application correct, etc.).


I first tried doing what you did, and the responder chain for key events is so complex (see the Apple docs) that I found it easier to take it "off the top" than get the events routed properly, especially since I was trying to modify the behavior of an NSTextField with its associated field editor (which is the actual first responder), etc.

Enjoy,

Doug


/** Overridden sendEvent so that we can handle a few different key presses
* differently **/
- (void)sendEvent:(NSEvent *)theEvent
{
if (controller && [controller attemptToHandleSentEvent:theEvent])
// Our controller handled this
return;
// Handle it the usual way
[super sendEvent:theEvent];
}



On Mar 23, 2007, at 7:55 PM, Timothy Mowlem wrote:

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:
40dpf.cc


This email sent to email@hidden


_______________________________________________

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


References: 
 >How to get key events correctly (From: Timothy Mowlem <email@hidden>)

  • Prev by Date: Re: [RESOLVED] disable mouse clicks and keyboard shortcuts in nsmovieview
  • Next by Date: Re: [SOLVED!] Progress Sheet on a NSDocument
  • Previous by thread: How to get key events correctly
  • Next by thread: Compositing shrunk cells
  • Index(es):
    • Date
    • Thread