user3482876
user3482876

Reputation: 249

python changing keyboard language in Mac OS X

I wrote python software that requires both german and english input. Currently, I am using the Mac shortcut cmd-space to quickly change keyboard layouts, but I need to do this hundreds of times, which slows things down. Since I know exactly when English vs German input is needed, is there a way to programmatically change the keyboard language in python?

I am using Mac OS X Yosemite.

Upvotes: 2

Views: 1140

Answers (3)

Denny Weinberg
Denny Weinberg

Reputation: 2606

Calling xkbswitch from python using subprocess would probably be easier than the other solutions you find here.

Upvotes: 0

Tiger J. Wang
Tiger J. Wang

Reputation: 1

TkTech offers some general pointers, but it appears that pyobjc doesn't actually wrap the two Carbon methods you need (TISCreateInputSourceList and TISSelectInputSource). In fact, pyobjc discontinued Carbon support altogether in Python 3.

So yes, pyobjc is going to be helpful for calling Objective-C code in Python, but you're going to have to manually wrap those two methods you need, probably using ctypes. The code to do this is lengthier than it should be and a bit tedious, so I'll just link you to my implementation.

Upvotes: 0

TkTech
TkTech

Reputation: 4996

Yes, but it isn't fun.

The method you're looking for is TISSelectInputSource and the related TIS methods. TIS is a Core Foundation library written in C. You will want to use pyobjc.

Once you're actually setup, the process is simple. Use TISCreateInputSourceList to get the TISInputSourceRef for the language you want to use and pass it to TISSelectInputSource.

Upvotes: 3

Related Questions