I am using a custom view class, which is a subclass of NSControl. This view is a key view and needs to handle keyDown event including space/tab key.
But, when Full Keyboard Access is enabled at System Settings app > Accessibility > Keyboard, the custom view cannot receive space/tab keyDown event. MacOS seems to interrupt space/tab key event to control keyboard focus.
Screenshot: Full Keyboard Access setting at Accessibility
On the other hand, NSTextView seems to be able to receive space/tab key events as follows even when Full Keyboard Access is enabled.
- When user presses tab key from the previous view, keyboard focus moves to the NSTextView.
- When user presses tab key at the NSTextView before inputing any letter, keyboard focus moves to the next view.
- When user presses tab key at the NSTextView after inputing any letter, a tab letter is input.
- When user presses space key at the NSTextView, a space letter is input.
I want to do the same behavior as NSTextView. But, I could not find the way to receive space/tab keyDown event when Full Keyboard Access is enabled.
How to receive keyDown event for space/tab key when Full Keyboard Access is enabled?
I checked the following post, but it seems that the answer only explains how to set key view loop so that keyboard focus can be moved.
I confirmed this issue happens with macOS 13.2.1 and 12.5.
There was an old "Full Keyboard Access" setting at System Settings app > Keyboard > Shortcuts in older macOS. This issue did not happen when the old Full Keyboard Access was enabled.
I tested the following minimal test code.
CustomView.m
@implementation CustomView
- (BOOL)acceptsFirstResponder {
return YES;
}
- (BOOL)canBecomeKeyView {
return YES;
}
- (void)keyDown:(NSEvent *)event {
NSLog(@"keyDown");
}
- (void)mouseDown:(NSEvent *)event {
[[self window] makeFirstResponder:self];
}
- (BOOL)isAccessibilityElement {
return YES;
}
- (BOOL)isAccessibilityEnabled {
return YES;
}
- (NSAccessibilityRole)accessibilityRole {
return NSAccessibilityTextFieldRole;
}
@end
CustomView.h
@interface CustomView : NSControl
@end
ViewController.m
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[[self view] window] makeFirstResponder:self.customView1];
}
@end
Expected result: The custom view can receive space/tab keyDown event.
Actual result: The custom view cannot receive space/tab keyDown event.