How to change the Mac OS X Keyboard Layout programmatically?

故事扮演 提交于 2021-02-07 09:33:17

问题


My Qt app supports changing input language on Linux and Windows. I want to add support for changing the input language in Mac OSX, too.

Unfortunately I don't have any information about the Mac SDK. (My first and last work with OS X was compiling Qt and compiling my app!)

I googling this problem and found that I need to use Text Input Source Services and also I found these codes:

TISInputSourceRef isref;
isref = TISCopyCurrentKeyboardInputSource();
CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(isref, kTISPropertyUnicodeKeyLayoutData);

UCKeyboardLayout * keyLayoutPtr=(UCKeyboardLayout*)CFDataGetBytePtr(uchr);

Is keyLayoutPtr a pointer for current keyboard layout?

If the answer to the above question is "yes", then what should I compare with keyLayoutPtr?

In other words, how can I check if the current keyboard layout is (for example) "English"? (Or something like LANG_ENGLISH in Win API or us in XLib.)


回答1:


I think you'd pass isref to TISGetInputSourceProperty, using the key kTISPropertyInputSourceLanguages, and check whether the first language code in the array is "en" or something like that. I don't know if it will return just "en" or "en-US" etc.



来源:https://stackoverflow.com/questions/3338781/how-to-change-the-mac-os-x-keyboard-layout-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!