@protocol NJKeyInputFieldDelegate;
-@interface NJKeyInputField : NSTextField
+@interface NJKeyInputField : NSControl
// An NJKeyInputField is a NSTextField-like widget that receives
// exactly one key press, and displays the name of that key, then
// resigns its first responder status. It can also inform a
+ (NSString *)stringForKeyCode:(CGKeyCode)keyCode;
// Give the string name for a virtual key code.
-@property (nonatomic, weak) IBOutlet id <NJKeyInputFieldDelegate, NSTextFieldDelegate> delegate;
+@property (nonatomic, weak) IBOutlet id <NJKeyInputFieldDelegate> delegate;
@property (nonatomic, assign) CGKeyCode keyCode;
// The currently displayed key code, or NJKeyInputFieldEmpty if no
@protocol NJKeyInputFieldDelegate <NSObject>
-@optional
- (void)keyInputField:(NJKeyInputField *)keyInput
didChangeKey:(CGKeyCode)keyCode;
- (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput;
const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX;
-@implementation NJKeyInputField
+@implementation NJKeyInputField {
+ NSTextField *field;
+}
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame:frameRect])) {
- self.alignment = NSCenterTextAlignment;
- self.editable = NO;
- self.selectable = NO;
+ field = [[NSTextField alloc] initWithFrame:self.bounds];
+ field.alignment = NSCenterTextAlignment;
+ field.editable = NO;
+ field.selectable = NO;
+ [self addSubview:field];
}
return self;
}
- (void)clear {
self.keyCode = NJKeyInputFieldEmpty;
- if ([self.delegate respondsToSelector:@selector(keyInputFieldDidClear:)])
[self.delegate keyInputFieldDidClear:self];
[self resignIfFirstResponder];
}
}
- (BOOL)becomeFirstResponder {
- self.backgroundColor = NSColor.selectedTextBackgroundColor;
+ field.backgroundColor = NSColor.selectedTextBackgroundColor;
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder {
- self.backgroundColor = NSColor.textBackgroundColor;
+ field.backgroundColor = NSColor.textBackgroundColor;
return [super resignFirstResponder];
}
- (void)setKeyCode:(CGKeyCode)keyCode {
_keyCode = keyCode;
- self.stringValue = [NJKeyInputField stringForKeyCode:keyCode];
+ field.stringValue = [NJKeyInputField stringForKeyCode:keyCode];
}
- (void)keyDown:(NSEvent *)event {
if ((event.modifierFlags & IGNORE) && event.keyCode == kVK_Delete) {
// Allow Alt/Command+Delete to clear the field.
self.keyCode = NJKeyInputFieldEmpty;
- if ([self.delegate respondsToSelector:@selector(keyInputFieldDidClear:)])
- [self.delegate keyInputFieldDidClear:self];
+ [self.delegate keyInputFieldDidClear:self];
} else if (!(event.modifierFlags & IGNORE)) {
self.keyCode = event.keyCode;
- if ([self.delegate respondsToSelector:@selector(keyInputField:didChangeKey:)])
- [self.delegate keyInputField:self didChangeKey:self.keyCode];
+ [self.delegate keyInputField:self didChangeKey:self.keyCode];
}
[self resignIfFirstResponder];
}
// modifiers are still down.
if (!(theEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) {
self.keyCode = theEvent.keyCode;
- if ([self.delegate respondsToSelector:@selector(keyInputField:didChangeKey:)])
- [self.delegate keyInputField:self didChangeKey:_keyCode];
+ [self.delegate keyInputField:self didChangeKey:_keyCode];
}
}
-- (void)setDelegate:(id<NJKeyInputFieldDelegate, NSTextFieldDelegate>)delegate {
- [super setDelegate:delegate];
-}
-
-- (id <NJKeyInputFieldDelegate, NSTextFieldDelegate>)delegate {
- return (id)[super delegate];
-}
-
@end