Cocoa key bindings now based on key codes

This commit is contained in:
Lior Halphon 2017-01-24 21:00:56 +02:00
parent 9779635c34
commit a5f72627fe
7 changed files with 71 additions and 47 deletions

View File

@ -1,8 +1,6 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#include "GBButtons.h"
@interface AppDelegate () #import <Carbon/Carbon.h>
@end
@implementation AppDelegate @implementation AppDelegate
{ {
@ -11,26 +9,31 @@
- (void) applicationDidFinishLaunching:(NSNotification *)notification - (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:@{ [[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"GBRight": KEY(NSRightArrowFunctionKey), @"GBRight": @(kVK_RightArrow),
@"GBLeft": KEY(NSLeftArrowFunctionKey), @"GBLeft": @(kVK_LeftArrow),
@"GBUp": KEY(NSUpArrowFunctionKey), @"GBUp": @(kVK_UpArrow),
@"GBDown": KEY(NSDownArrowFunctionKey), @"GBDown": @(kVK_DownArrow),
@"GBA": @"x", @"GBA": @(kVK_ANSI_X),
@"GBB": @"z", @"GBB": @(kVK_ANSI_Z),
@"GBSelect": @"\x7f", @"GBSelect": @(kVK_Delete),
@"GBStart": @"\r", @"GBStart": @(kVK_Return),
@"GBTurbo": @" ", @"GBTurbo": @(kVK_Space),
@"GBFilter": @"NearestNeighbor", @"GBFilter": @"NearestNeighbor",
}]; }];
#undef KEY
} }
- (IBAction)toggleDeveloperMode:(id)sender { - (IBAction)toggleDeveloperMode:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:![defaults boolForKey:@"DeveloperMode"] forKey:@"DeveloperMode"]; [defaults setBool:![defaults boolForKey:@"DeveloperMode"] forKey:@"DeveloperMode"];
} }

View File

@ -1,6 +1,7 @@
#import "GBPreferencesWindow.h" #import "GBPreferencesWindow.h"
#import "NSString+StringForKey.h" #import "NSString+StringForKey.h"
#import "GBButtons.h" #import "GBButtons.h"
#import <Carbon/Carbon.h>
@implementation GBPreferencesWindow @implementation GBPreferencesWindow
{ {
@ -60,7 +61,7 @@
return @"Select a new key..."; return @"Select a new key...";
} }
return [NSString displayStringForKeyString:[[NSUserDefaults standardUserDefaults] stringForKey: return [NSString displayStringForKeyCode:[[NSUserDefaults standardUserDefaults] integerForKey:
button_to_preference_name(row)]]; button_to_preference_name(row)]];
} }
@ -87,7 +88,7 @@
is_button_being_modified = false; is_button_being_modified = false;
[[NSUserDefaults standardUserDefaults] setObject:theEvent.charactersIgnoringModifiers [[NSUserDefaults standardUserDefaults] setInteger:theEvent.keyCode
forKey:button_to_preference_name(button_being_modified)]; forKey:button_to_preference_name(button_being_modified)];
self.controlsTableView.enabled = YES; self.controlsTableView.enabled = YES;
[self.controlsTableView reloadData]; [self.controlsTableView reloadData];
@ -119,4 +120,15 @@
[_aspectRatioCheckbox setState: ![[NSUserDefaults standardUserDefaults] boolForKey:@"GBAspectRatioUnkept"]]; [_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 @end

View File

@ -163,11 +163,12 @@
-(void)keyDown:(NSEvent *)theEvent -(void)keyDown:(NSEvent *)theEvent
{ {
unsigned short keyCode = theEvent.keyCode;
bool handled = false; bool handled = false;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (GBButton i = 0; i < GBButtonCount; i++) { 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; handled = true;
switch (i) { switch (i) {
case GBTurbo: case GBTurbo:
@ -188,11 +189,12 @@
-(void)keyUp:(NSEvent *)theEvent -(void)keyUp:(NSEvent *)theEvent
{ {
unsigned short keyCode = theEvent.keyCode;
bool handled = false; bool handled = false;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (GBButton i = 0; i < GBButtonCount; i++) { 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; handled = true;
switch (i) { switch (i) {
case GBTurbo: 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 - (BOOL)acceptsFirstResponder
{ {
return YES; return YES;

View File

@ -1,7 +1,7 @@
#ifndef NSKeyboardShortcut_h #ifndef KeyboardShortcutPrivateAPIs_h
#define NSKeyboardShortcut_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. */ to convert a key equivalent to its display name. */
@interface NSKeyboardShortcut : NSObject <NSCopying> @interface NSKeyboardShortcut : NSObject <NSCopying>
@ -19,4 +19,8 @@
@end @end
@interface NSPrefPaneUtils : NSObject
+ (id)stringForVirtualKey:(unsigned int)key modifiers:(unsigned int)flags;
@end
#endif #endif

View File

@ -2,4 +2,5 @@
@interface NSString (StringForKey) @interface NSString (StringForKey)
+ (NSString *) displayStringForKeyString: (NSString *)key_string; + (NSString *) displayStringForKeyString: (NSString *)key_string;
+ (NSString *) displayStringForKeyCode:(unsigned short) keyCode;
@end @end

View File

@ -1,12 +1,35 @@
#import "NSString+StringForKey.h" #import "NSString+StringForKey.h"
#import "NSKeyboardShortcut.h" #import "KeyboardShortcutPrivateAPIs.h"
#import <Carbon/Carbon.h>
@implementation NSString (StringForKey) @implementation NSString (StringForKey)
+ (NSString *) displayStringForKeyString: (NSString *)key_string + (NSString *) displayStringForKeyString: (NSString *)key_string
{ {
return [[NSKeyboardShortcut shortcutWithKeyEquivalent:key_string modifierMask:0] localizedDisplayName]; 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 @end

View File

@ -49,7 +49,7 @@ ifeq ($(PLATFORM),Darwin)
SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> /dev/null) SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> /dev/null)
CFLAGS += -F/Library/Frameworks CFLAGS += -F/Library/Frameworks
OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) -mmacosx-version-min=10.9 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 SDL_LDFLAGS := -framework SDL
endif endif