Created
October 5, 2010 03:42
-
-
Save knu/610948 to your computer and use it in GitHub Desktop.
Swap Cmd and Option in iTerm2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/iTermApplication.m b/iTermApplication.m | |
index b9d47f1..8d16797 100644 | |
--- a/iTermApplication.m | |
+++ b/iTermApplication.m | |
@@ -101,13 +101,30 @@ | |
return; | |
} | |
+ unsigned short keyCode = [event keyCode]; | |
+ unsigned int modifierFlags = [event modifierFlags]; | |
const int mask = NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask | NSCommandKeyMask; | |
- if (([event modifierFlags] & mask) == NSCommandKeyMask) { | |
- int digit = [[event charactersIgnoringModifiers] intValue]; | |
- if (digit >= 1 && digit <= [tabView numberOfTabViewItems]) { | |
- // Command+number: Switch to tab by number. | |
- [tabView selectTabViewItemAtIndex:digit-1]; | |
- return; | |
+ int uiModifier = NSCommandKeyMask; | |
+ int commandKeyAsMeta = 1; | |
+ | |
+ if (commandKeyAsMeta) { | |
+ uiModifier = NSAlternateKeyMask; | |
+ } | |
+ | |
+ if((modifierFlags & mask) == uiModifier) { | |
+ switch (keyCode) | |
+ { | |
+ case 0x12: case 0x13: case 0x14: // 1 2 3 | |
+ case 0x15: case 0x17: case 0x16: // 4 5 6 | |
+ case 0x1a: case 0x1c: case 0x19: // 7 8 9 | |
+ { | |
+ // Command+number: Switch to tab by number. | |
+ int digit = [[event charactersIgnoringModifiers] intValue]; | |
+ if (digit >= 1 && digit <= [tabView numberOfTabViewItems]) { | |
+ [tabView selectTabViewItemAtIndex:digit-1]; | |
+ return; | |
+ } | |
+ } | |
} | |
} | |
@@ -117,6 +134,60 @@ | |
[currentSession keyDown:event]; | |
return; | |
} | |
+ | |
+ if (commandKeyAsMeta) { | |
+ NSString* charactersIgnoringModifiers = [event charactersIgnoringModifiers]; | |
+ if((modifierFlags & NSCommandKeyMask) == NSCommandKeyMask) { | |
+ if((modifierFlags & NSAlternateKeyMask) != NSAlternateKeyMask) { | |
+ NSString* keystr = [event characters]; | |
+ NSEvent* newEvent; | |
+ | |
+ switch (keyCode) | |
+ { | |
+ case 0x31: // space | |
+ case 0x24: // enter | |
+ break; | |
+ | |
+ default: | |
+ modifierFlags -= NSCommandKeyMask; | |
+ modifierFlags |= NSLeftAlternateKeyMask; | |
+ newEvent = [NSEvent keyEventWithType: [event type] | |
+ location: [event locationInWindow] | |
+ modifierFlags: modifierFlags | |
+ timestamp: [event timestamp] | |
+ windowNumber: [event windowNumber] | |
+ context: [event context] | |
+ characters: keystr | |
+ charactersIgnoringModifiers: charactersIgnoringModifiers | |
+ isARepeat: [event isARepeat] | |
+ keyCode: keyCode]; | |
+ [currentSession keyDown: newEvent]; | |
+ return; | |
+ } | |
+ } | |
+ } else if((modifierFlags & NSAlternateKeyMask) == NSAlternateKeyMask) { | |
+ NSEvent* newEvent; | |
+ NSString* newstr = charactersIgnoringModifiers; | |
+ | |
+ modifierFlags -= NSAlternateKeyMask; | |
+ modifierFlags |= NSCommandKeyMask; | |
+ newEvent = [NSEvent keyEventWithType: [event type] | |
+ location: [event locationInWindow] | |
+ modifierFlags: modifierFlags | |
+ timestamp: [event timestamp] | |
+ windowNumber: [event windowNumber] | |
+ context: [event context] | |
+ characters: newstr | |
+ charactersIgnoringModifiers: newstr | |
+ isARepeat: [event isARepeat] | |
+ keyCode: keyCode]; | |
+ [super sendEvent: newEvent]; | |
+ return; | |
+ } | |
+ } | |
+ } else { | |
+ [super sendEvent: event]; | |
+ return; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment