Created
June 20, 2016 20:14
-
-
Save knugie/6202f13545917c4fb3042c642f3a14c7 to your computer and use it in GitHub Desktop.
Keyboard layout switcher - Xcode project (objective-c, command line tool)
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
// | |
// main.m | |
// keyboard_layout_switcher | |
// | |
@import Carbon; | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
if (argc <= 1){ | |
printf("Missing argument: Pass 'US' or 'German'.\n"); | |
exit(1); | |
} | |
NSString *layout = [NSString stringWithUTF8String:argv[1]]; | |
if ([layout isEqualToString: @"US"] || [layout isEqualToString: @"German"]) { | |
NSArray* sources = CFBridgingRelease(TISCreateInputSourceList((__bridge CFDictionaryRef)@{ (__bridge NSString*)kTISPropertyInputSourceID : [NSString stringWithFormat:@"com.apple.keylayout.%@", layout]}, FALSE)); | |
TISInputSourceRef source = (__bridge TISInputSourceRef)sources[0]; | |
OSStatus status = TISSelectInputSource(source); | |
if (status != noErr){ | |
printf("Unexpected Error! Could not switch keyboard layout,"); | |
} else { | |
printf("Switched keyboard layout to %s.\n", [layout UTF8String]); | |
} | |
} else { | |
printf("Invalid argument: Pass 'US' or 'German'.\n"); | |
exit(1); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment