How to handle cocoa arrow key events without warning sound?

Hi,


First I tried - (void)keyDown:(NSEvent *) in a subclass of NSView, all other keys works but now arrow keys.


I also tried addLocalMonitorForEventsMatchingMask, I can success captured arrow keys, but system warning with 'funk' sound when I press arrow key.


[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown

handler:^NSEvent * _Nullable(NSEvent * event) {

//check if it is arrow key

return event;

}];

- (BOOL)performKeyEquivalent:(NSEvent *)event {

return YES;

}


I'm not able to find a answer from https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/HandlingKeyEvents/HandlingKeyEvents.html

This solution is so simple - you might (and you will) laugh about it.
just return nil;
Code Block
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent * _Nullable(NSEvent * event) {
switch (event.keyCode) {
case 126: //up
case 125: { return nil; } break; //down
default: {
/* what ever you want to do with all other keys goes here */
} break;
}
return event;
}];

for the specific event.keyCode to stop this exact event from passing on to your event handling system (NSResponder).
See how this example still returns all other events, so your UI does not become unresponsive to all other key events.
How to handle cocoa arrow key events without warning sound?
 
 
Q