Unix/Mac equivalent to GetKeyboardLayoutName?

Recommended Posts

myers80    194

Is there a unix and mac equivalent to the window's function GetKeyboardLayoutName, which retrieve's the system's active input locale identifier?  Thanks!

Share on other sites
swiftcoder    18437

I don't really think that Mac/Linux have the concept of an input locale that is separate from the system locale, but I could be mistaken.

If you just want the current system locale, something like this should be sufficient:

#include <locale>

int main()
{
std::locale current;
std::cout << current.name() << std::endl;
}


You could also call [tt]getenv("LANG")[/tt] to query the shell's locale.

Share on other sites
myers80    194

I don't really think that Mac/Linux have the concept of an input locale that is separate from the system locale, but I could be mistaken.

If you just want the current system locale, something like this should be sufficient:

#include <locale>

int main()
{
std::locale current;
std::cout << current.name() << std::endl;
}


You could also call [tt]getenv("LANG")[/tt] to query the shell's locale.

I'm not sure the system locale is what I'm looking for.  I need a way to determine what the keyboard's layout is (i.e. qwerty or dvorak).  So far it looks like XkbLayout might have this information.  Does anyone know if that is the case?

Share on other sites
swiftcoder    18437

I need a way to determine what the keyboard's layout is (i.e. qwerty or dvorak).

Ah, sorry, the use of the term 'locale' confused me.

On Mac you need to use the Text Input Source Services.

On Linux things are a bit of a nightmare, maybe the source of the xkb-switch utility will be useful to you. I'm fairly sure that this isn't possible to accomplish in a robust fashion under X, however - on my box, Gnome seems to disagree with X about the active keyboard layout.

Share on other sites
myers80    194

A bummer that Linux does not make it easy.  I will continue to look into it and report back if I figure it out.  Thanks for the reply.

Create an account

Register a new account