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"
@interface AppDelegate ()
@end
#include "GBButtons.h"
#import <Carbon/Carbon.h>
@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"];
}

View File

@ -1,6 +1,7 @@
#import "GBPreferencesWindow.h"
#import "NSString+StringForKey.h"
#import "GBButtons.h"
#import <Carbon/Carbon.h>
@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

View File

@ -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;

View File

@ -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 <NSCopying>
@ -19,4 +19,8 @@
@end
@interface NSPrefPaneUtils : NSObject
+ (id)stringForVirtualKey:(unsigned int)key modifiers:(unsigned int)flags;
@end
#endif

View File

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

View File

@ -1,12 +1,35 @@
#import "NSString+StringForKey.h"
#import "NSKeyboardShortcut.h"
#import "KeyboardShortcutPrivateAPIs.h"
#import <Carbon/Carbon.h>
@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

View File

@ -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