From c63310ae5acaaeb7feb6dc63ba8029683655d75a Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Tue, 12 Mar 2013 14:00:38 +0100 Subject: [PATCH] NJKeyInputField descends from NSControl and has a text field, rather than being one. --- Classes/NJKeyInputField.h | 5 ++--- Classes/NJKeyInputField.m | 36 ++++++++++++++---------------------- Info.plist | 2 +- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/Classes/NJKeyInputField.h b/Classes/NJKeyInputField.h index 84498ff..c17fdaa 100644 --- a/Classes/NJKeyInputField.h +++ b/Classes/NJKeyInputField.h @@ -11,7 +11,7 @@ extern const CGKeyCode NJKeyInputFieldEmpty; @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 @@ -20,7 +20,7 @@ extern const CGKeyCode NJKeyInputFieldEmpty; + (NSString *)stringForKeyCode:(CGKeyCode)keyCode; // Give the string name for a virtual key code. -@property (nonatomic, weak) IBOutlet id delegate; +@property (nonatomic, weak) IBOutlet id delegate; @property (nonatomic, assign) CGKeyCode keyCode; // The currently displayed key code, or NJKeyInputFieldEmpty if no @@ -37,7 +37,6 @@ extern const CGKeyCode NJKeyInputFieldEmpty; @protocol NJKeyInputFieldDelegate -@optional - (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode; - (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput; diff --git a/Classes/NJKeyInputField.m b/Classes/NJKeyInputField.m index 2bdbfc6..4e5513a 100644 --- a/Classes/NJKeyInputField.m +++ b/Classes/NJKeyInputField.m @@ -17,20 +17,23 @@ enum { 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]; } @@ -189,18 +192,18 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX; } - (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 { @@ -209,12 +212,10 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX; 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]; } @@ -236,17 +237,8 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX; // 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)delegate { - [super setDelegate:delegate]; -} - -- (id )delegate { - return (id)[super delegate]; -} - @end diff --git a/Info.plist b/Info.plist index a8c5eb5..9bb0e57 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 183 + 184 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright -- 2.30.2