Re: performKeyEquivalent disables Command-Q hotkey
Re: performKeyEquivalent disables Command-Q hotkey
- Subject: Re: performKeyEquivalent disables Command-Q hotkey
- From: Ricky Sharp <email@hidden>
- Date: Wed, 14 Jul 2004 11:18:28 -0500
On Wednesday, July 14, 2004, at 10:43AM, Michael Becker <email@hidden> wrote:
>
> I have a question concerning NSView's performKeyEquivalent:.
>
> In my NSView subclass, I implement performKeyEquivalent in order to
>
> react to the user pressing certain keys. However, this seems to kill
>
> the standard Command-Q (Quit application) functionality. I tried
>
> invoking [ super performKeyEquivalent:] before doing my own stuff, but
>
> that didn't work either. Here's my bit of code:
>
>
>
> - (BOOL)performKeyEquivalent:(NSEvent*)event {
>
> if ([ super performKeyEquivalent:event]) {
>
> return YES;
>
> } else {
>
> unichar theChar = [[ event charactersIgnoringModifiers]
>
> characterAtIndex:0];
>
>
>
> // Select all images on COMMAND-A
>
> if (( theChar == 'a' ) && ( [ event modifierFlags] &
>
> NSCommandKeyMask )) {
>
> [ self selectAllImages];
>
> [ self setNeedsDisplay:YES];
>
> return YES;
>
> }
>
> }
>
> return NO;
>
> }
I would probably structure this a bit differently (according the notes about the API): e.g. you should first see if the equivalent is something that you handle. If so, you return YES. Otherwise, you need to call through to the superclass.
So, the code should look something like this:
- (BOOL) performKeyEquivalent:(NSEvent*) event
{
unichar theChar = [[ event charactersIgnoringModifiers] characterAtIndex:0];
if (( theChar == 'a' ) && ( [ event modifierFlags] & NSCommandKeyMask ))
// do your work here and return YES
else
return [super performKeyEquivalent:event];
}
I'm very new to Cocoa development, but is this the best method of trapping on common commands? Do you have a Select All in your Edit menu? If so, I think it would be better to update the status of that item dependent upon your state and then execute some action if that menu item is selected.
Rick Sharp
Instant Interactive(tm)
_______________________________________________
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.