Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Keyboard Layout (a X11 API solution)

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

like image 528
S. Razi Avatar asked Feb 26 '23 16:02

S. Razi


1 Answers

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
like image 195
S. Razi Avatar answered Mar 02 '23 05:03

S. Razi