views:

142

answers:

2

I want to change keyboard layout in Linux by programming, What X11's API function does this?

+1  A: 

I'm not sure what the X11 library function is but setxkbmap is the bash command I use to achieve it. Maybe searching along these lines will find what you want (or at a pinch you could just execute the bash command).

Example

setxkbmap dvorak
setxkbmap us

EDIT: After a strace of setxkbmap didn't turn up anything useful I suggest just calling:

system(“setxkbmap us”);
Daniel
@Daniel, you could have at least rephrased the answer to sound like "look into the source code of setxkbmap". I would have posted the answer yesterday - if only I had time to find the sources. Likewise checking sources of the keyboard switching applets in KDE/Gnome/Xfce.
Dummy00001
@Daniel: yes it works, but if there is more than one group installed on computer then using "setxkbmap us" will disable all other languages.
Razi
@Dummy00001: I look into the source code of setxkbmap before asking this question, it's very "low level" for my Linux programming knowledge!
Razi
A: 

I found one good solution. It's a c++ class wrriten by Jay Bromley, that I can add to my app and using it.

source code

It's very easy to use:

#include "XKeyboard.h"

XKeyboard xkb;

std::string cGrpName=xkb.currentGroupName(); //return somethings like "USA"
std::string cGrpSymb=xkb.currentGroupSymbol(); //return somethings like "us"

xkb.setGroupByNum(0);//set keyboard layout to first layout in available ones

you can read source code and found some another useful functions. for compiling standalone version you need to un-comments "int main" function present in "XKeyboard.cpp" (or write your own main.cpp) and use somethings like this:

g++ *.cpp -o getxkblayout -L/usr/lib -lX11
Razi