Skip to content

Instantly share code, notes, and snippets.

@knugie
Created June 20, 2016 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knugie/6202f13545917c4fb3042c642f3a14c7 to your computer and use it in GitHub Desktop.
Save knugie/6202f13545917c4fb3042c642f3a14c7 to your computer and use it in GitHub Desktop.
Keyboard layout switcher - Xcode project (objective-c, command line tool)
//
// 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