From: Joe Wreschnig Date: Tue, 12 Mar 2013 13:38:17 +0000 (+0100) Subject: Allow Command+Click to enter a raw key code. X-Git-Tag: version-1.1~45 X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;ds=inline;h=0bc6bfcffe3319a339e28e64a81600a85382d61e;hp=c63310ae5acaaeb7feb6dc63ba8029683655d75a;p=enjoyable.git Allow Command+Click to enter a raw key code. --- diff --git a/Classes/NJKeyInputField.h b/Classes/NJKeyInputField.h index c17fdaa..c32e16c 100644 --- a/Classes/NJKeyInputField.h +++ b/Classes/NJKeyInputField.h @@ -11,7 +11,7 @@ extern const CGKeyCode NJKeyInputFieldEmpty; @protocol NJKeyInputFieldDelegate; -@interface NJKeyInputField : NSControl +@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 diff --git a/Classes/NJKeyInputField.m b/Classes/NJKeyInputField.m index 4e5513a..02896a8 100644 --- a/Classes/NJKeyInputField.m +++ b/Classes/NJKeyInputField.m @@ -19,6 +19,7 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX; @implementation NJKeyInputField { NSTextField *field; + NSImageView *warning; } - (id)initWithFrame:(NSRect)frameRect { @@ -27,7 +28,21 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX; field.alignment = NSCenterTextAlignment; field.editable = NO; field.selectable = NO; + field.delegate = self; [self addSubview:field]; + + warning = [[NSImageView alloc] init]; + warning.image = [NSImage imageNamed:@"NSInvalidDataFreestanding"]; + CGSize imgSize = warning.image.size; + CGRect bounds = self.bounds; + warning.frame = CGRectMake(bounds.size.width - (imgSize.width + 4), + (bounds.size.height - imgSize.height) / 2, + imgSize.width, imgSize.height); + + warning.toolTip = NSLocalizedString(@"invalid key code", + @"shown when the user types an invalid key code"); + warning.hidden = YES; + [self addSubview:warning]; } return self; } @@ -220,12 +235,51 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX; [self resignIfFirstResponder]; } } + +static BOOL isValidKeyCode(long code) { + return code < 0xFFFF && code >= 0; +} + +- (void)controlTextDidChange:(NSNotification *)obj { + char *error = NULL; + long code = strtol(field.stringValue.UTF8String, &error, 16); + warning.hidden = (isValidKeyCode(code) && !*error) || !field.stringValue.length; +} + +- (void)controlTextDidEndEditing:(NSNotification *)obj { + [field.cell setPlaceholderString:@""]; + field.editable = NO; + field.selectable = NO; + warning.hidden = YES; + char *error = NULL; + const char *s = field.stringValue.UTF8String; + long code = strtol(s, &error, 16); + if (!*error && isValidKeyCode(code) && field.stringValue.length) { + self.keyCode = code; + [self.delegate keyInputField:self didChangeKey:self.keyCode]; + } else { + self.keyCode = self.keyCode; + } +} + - (void)mouseDown:(NSEvent *)theEvent { - if (self.window.firstResponder == self) - [self.window makeFirstResponder:nil]; - else if (self.acceptsFirstResponder) - [self.window makeFirstResponder:self]; + if (self.isEnabled) { + if (theEvent.modifierFlags & NSCommandKeyMask) { + field.editable = YES; + field.selectable = YES; + field.stringValue = @""; + [field.cell setPlaceholderString: + NSLocalizedString(@"enter key code", + @"shown when user must enter a key code to map to")]; + [self.window makeFirstResponder:field]; + } else { + if (self.window.firstResponder == self) + [self.window makeFirstResponder:nil]; + else if (self.acceptsFirstResponder) + [self.window makeFirstResponder:self]; + } + } } - (void)flagsChanged:(NSEvent *)theEvent { @@ -235,7 +289,8 @@ const CGKeyCode NJKeyInputFieldEmpty = kVK_MAX; // user type these virtual keys. However, there is no actual event // for modifier key up - so detect it by checking to see if any // modifiers are still down. - if (!(theEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) { + if (!field.isEditable + && !(theEvent.modifierFlags & NSDeviceIndependentModifierFlagsMask)) { self.keyCode = theEvent.keyCode; [self.delegate keyInputField:self didChangeKey:_keyCode]; } diff --git a/Info.plist b/Info.plist index 9bb0e57..c72bc22 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 184 + 185 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright diff --git a/Resources/English.lproj/Localizable.strings b/Resources/English.lproj/Localizable.strings index b1dda71..91f6bf7 100644 Binary files a/Resources/English.lproj/Localizable.strings and b/Resources/English.lproj/Localizable.strings differ