How do I programmatically change the keyboard layout using Cocoa?

How can I programmatically change the keyboard layout in Cocoa?

Suppose I have, say, two active "Estonian" and "American" in system preferences (ie these two layouts are displayed in the keyboard layout menu bar).

So how would I read that these two are available, and how would I programmatically change them between each other?

+2


source to share


1 answer


Use Text Input Services if you're on 10.5 and up. It's based on CoreFoundation, so don't worry about it within Carbon. If you need to support 10.4 or older, you need to use "Keyboard Layouts" . I only know the first, so let me just explain that.

You're using

CFArrayRef sourceList= TISCreateInputSourceList (NULL,false);

      



to get an array TISInputSourceRef

corresponding to all included keyboard types. Then you can use other functions to explore them. To select / deselect, you can use TISSelectInputSource

and TISDeselectInputSource

etc.

Don't forget the CFRelease

array you have, even in a garbage collection environment, as CF objects are not garbage collected automatically!

+3


source







All Articles