1

I need to get the keyboard layout being used on Mac OS X. I've found old restonces to this but they all are something like this:

How to change the Mac OS X Keyboard Layout programmatically?

which doesn't work any more. Any help would be much appreciated.

Daniel
  • 139
  • 13

2 Answers2

3

In case someone is interested, I was finally able to get the keyboard layout with the following code:

#include <Carbon/Carbon.h>
int main() {
    char layout[128];
    memset(layout, '\0', sizeof(layout));
    TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
    // get input source id - kTISPropertyInputSourceID
    // get layout name - kTISPropertyLocalizedName
    CFStringRef layoutID = TISGetInputSourceProperty(source, kTISPropertyInputSourceID);
    CFStringGetCString(layoutID, layout, sizeof(layout), kCFStringEncodingUTF8);
    printf("%s\n", layout);
    return 0;
}

Compiled with:

gcc -o test2 test2.c -framework Carbon

Daniel
  • 139
  • 13
0

I would try this:

#import <Carbon/Carbon.h>

TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
NSString *s = (__bridge NSString *)(TISGetInputSourceProperty(source, kTISPropertyInputSourceID));