From a5f72627fe1c6f38333ca94fa0bceca653d720e0 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 24 Jan 2017 21:00:56 +0200 Subject: [PATCH] Cocoa key bindings now based on key codes --- Cocoa/AppDelegate.m | 35 ++++++++++--------- Cocoa/GBPreferencesWindow.m | 16 +++++++-- Cocoa/GBView.m | 27 +++----------- ...ortcut.h => KeyboardShortcutPrivateAPIs.h} | 10 ++++-- Cocoa/NSString+StringForKey.h | 1 + Cocoa/NSString+StringForKey.m | 27 ++++++++++++-- Makefile | 2 +- 7 files changed, 71 insertions(+), 47 deletions(-) rename Cocoa/{NSKeyboardShortcut.h => KeyboardShortcutPrivateAPIs.h} (71%) diff --git a/Cocoa/AppDelegate.m b/Cocoa/AppDelegate.m index ff98f3f6..7a69b562 100644 --- a/Cocoa/AppDelegate.m +++ b/Cocoa/AppDelegate.m @@ -1,8 +1,6 @@ #import "AppDelegate.h" - -@interface AppDelegate () - -@end +#include "GBButtons.h" +#import @implementation AppDelegate { @@ -11,26 +9,31 @@ - (void) applicationDidFinishLaunching:(NSNotification *)notification { -#define KEY(x) ({unichar __x = x; [NSString stringWithCharacters:&(__x) length:1];}) + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + for (unsigned i = 0; i < GBButtonCount; i++) { + if ([[defaults objectForKey:button_to_preference_name(i)] isKindOfClass:[NSString class]]) { + [defaults removeObjectForKey:button_to_preference_name(i)]; + } + } [[NSUserDefaults standardUserDefaults] registerDefaults:@{ - @"GBRight": KEY(NSRightArrowFunctionKey), - @"GBLeft": KEY(NSLeftArrowFunctionKey), - @"GBUp": KEY(NSUpArrowFunctionKey), - @"GBDown": KEY(NSDownArrowFunctionKey), + @"GBRight": @(kVK_RightArrow), + @"GBLeft": @(kVK_LeftArrow), + @"GBUp": @(kVK_UpArrow), + @"GBDown": @(kVK_DownArrow), - @"GBA": @"x", - @"GBB": @"z", - @"GBSelect": @"\x7f", - @"GBStart": @"\r", + @"GBA": @(kVK_ANSI_X), + @"GBB": @(kVK_ANSI_Z), + @"GBSelect": @(kVK_Delete), + @"GBStart": @(kVK_Return), - @"GBTurbo": @" ", + @"GBTurbo": @(kVK_Space), @"GBFilter": @"NearestNeighbor", }]; -#undef KEY } -- (IBAction)toggleDeveloperMode:(id)sender { +- (IBAction)toggleDeveloperMode:(id)sender +{ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:![defaults boolForKey:@"DeveloperMode"] forKey:@"DeveloperMode"]; } diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index aa04b73a..689e6007 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -1,6 +1,7 @@ #import "GBPreferencesWindow.h" #import "NSString+StringForKey.h" #import "GBButtons.h" +#import @implementation GBPreferencesWindow { @@ -60,7 +61,7 @@ return @"Select a new key..."; } - return [NSString displayStringForKeyString:[[NSUserDefaults standardUserDefaults] stringForKey: + return [NSString displayStringForKeyCode:[[NSUserDefaults standardUserDefaults] integerForKey: button_to_preference_name(row)]]; } @@ -87,7 +88,7 @@ is_button_being_modified = false; - [[NSUserDefaults standardUserDefaults] setObject:theEvent.charactersIgnoringModifiers + [[NSUserDefaults standardUserDefaults] setInteger:theEvent.keyCode forKey:button_to_preference_name(button_being_modified)]; self.controlsTableView.enabled = YES; [self.controlsTableView reloadData]; @@ -119,4 +120,15 @@ [_aspectRatioCheckbox setState: ![[NSUserDefaults standardUserDefaults] boolForKey:@"GBAspectRatioUnkept"]]; } +- (void)awakeFromNib +{ + [super awakeFromNib]; + [[NSDistributedNotificationCenter defaultCenter] addObserver:self.controlsTableView selector:@selector(reloadData) name:(NSString*)kTISNotifySelectedKeyboardInputSourceChanged object:nil]; +} + +- (void)dealloc +{ + [[NSDistributedNotificationCenter defaultCenter] removeObserver:self.controlsTableView]; +} + @end diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 0053c667..316d95fc 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -163,11 +163,12 @@ -(void)keyDown:(NSEvent *)theEvent { + unsigned short keyCode = theEvent.keyCode; bool handled = false; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; for (GBButton i = 0; i < GBButtonCount; i++) { - if ([[defaults stringForKey:button_to_preference_name(i)] isEqualToString:theEvent.charactersIgnoringModifiers]) { + if ([defaults integerForKey:button_to_preference_name(i)] == keyCode) { handled = true; switch (i) { case GBTurbo: @@ -188,11 +189,12 @@ -(void)keyUp:(NSEvent *)theEvent { + unsigned short keyCode = theEvent.keyCode; bool handled = false; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; for (GBButton i = 0; i < GBButtonCount; i++) { - if ([[defaults stringForKey:button_to_preference_name(i)] isEqualToString:theEvent.charactersIgnoringModifiers]) { + if ([defaults integerForKey:button_to_preference_name(i)] == keyCode) { handled = true; switch (i) { case GBTurbo: @@ -211,27 +213,6 @@ } } -- (BOOL)becomeFirstResponder -{ - /* Non-Roman keyboard layouts breaks user input. */ - TSMDocumentID document = TSMGetActiveDocument(); - - CFArrayRef inpu_sources = TISCreateASCIICapableInputSourceList(); - TSMSetDocumentProperty(document, kTSMDocumentEnabledInputSourcesPropertyTag, - sizeof(CFArrayRef), &inpu_sources); - CFRelease(inpu_sources); - - return [super becomeFirstResponder]; -} - -- (BOOL)resignFirstResponder -{ - TSMDocumentID document = TSMGetActiveDocument(); - TSMRemoveDocumentProperty(document, kTSMDocumentEnabledInputSourcesPropertyTag); - - return [super resignFirstResponder]; -} - - (BOOL)acceptsFirstResponder { return YES; diff --git a/Cocoa/NSKeyboardShortcut.h b/Cocoa/KeyboardShortcutPrivateAPIs.h similarity index 71% rename from Cocoa/NSKeyboardShortcut.h rename to Cocoa/KeyboardShortcutPrivateAPIs.h index 347fc76e..a80dfde9 100644 --- a/Cocoa/NSKeyboardShortcut.h +++ b/Cocoa/KeyboardShortcutPrivateAPIs.h @@ -1,7 +1,7 @@ -#ifndef NSKeyboardShortcut_h -#define NSKeyboardShortcut_h +#ifndef KeyboardShortcutPrivateAPIs_h +#define KeyboardShortcutPrivateAPIs_h -/* This is private API, but it is a very simple and comprehensive way +/* These are private APIs, but they are a very simple and comprehensive way to convert a key equivalent to its display name. */ @interface NSKeyboardShortcut : NSObject @@ -19,4 +19,8 @@ @end +@interface NSPrefPaneUtils : NSObject ++ (id)stringForVirtualKey:(unsigned int)key modifiers:(unsigned int)flags; +@end + #endif \ No newline at end of file diff --git a/Cocoa/NSString+StringForKey.h b/Cocoa/NSString+StringForKey.h index d8e872b9..11aabd2d 100644 --- a/Cocoa/NSString+StringForKey.h +++ b/Cocoa/NSString+StringForKey.h @@ -2,4 +2,5 @@ @interface NSString (StringForKey) + (NSString *) displayStringForKeyString: (NSString *)key_string; ++ (NSString *) displayStringForKeyCode:(unsigned short) keyCode; @end diff --git a/Cocoa/NSString+StringForKey.m b/Cocoa/NSString+StringForKey.m index fb8fc09f..6126c584 100644 --- a/Cocoa/NSString+StringForKey.m +++ b/Cocoa/NSString+StringForKey.m @@ -1,12 +1,35 @@ #import "NSString+StringForKey.h" -#import "NSKeyboardShortcut.h" +#import "KeyboardShortcutPrivateAPIs.h" +#import @implementation NSString (StringForKey) + (NSString *) displayStringForKeyString: (NSString *)key_string { - return [[NSKeyboardShortcut shortcutWithKeyEquivalent:key_string modifierMask:0] localizedDisplayName]; } ++ (NSString *) displayStringForKeyCode:(unsigned short) keyCode +{ + /* These cases are not handled by stringForVirtualKey */ + switch (keyCode) { + case 115: return @"↖"; + case 119: return @"↘"; + case 116: return @"⇞"; + case 121: return @"⇟"; + case 51: return @"⌫"; + case 117: return @"⌦"; + case 76: return @"⌤"; + + /* Label Keypad buttons accordingly */ + default: + if ((keyCode < 82 || keyCode > 92)) { + return [NSPrefPaneUtils stringForVirtualKey:keyCode modifiers:0]; + } + + case 65: case 67: case 69: case 75: case 78: case 81: + return [@"Keypad " stringByAppendingString:[NSPrefPaneUtils stringForVirtualKey:keyCode modifiers:0]]; + } +} + @end diff --git a/Makefile b/Makefile index 8b21e45a..5e749c6a 100755 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ ifeq ($(PLATFORM),Darwin) SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> /dev/null) CFLAGS += -F/Library/Frameworks OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) -mmacosx-version-min=10.9 -LDFLAGS += -framework AppKit -framework Carbon -framework QuartzCore +LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore SDL_LDFLAGS := -framework SDL endif