How to change Keyboard Layout (a X11 API solution)

前端 未结 4 2213
Happy的楠姐
Happy的楠姐 2020-12-17 05:15

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

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 06:05

    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
    

提交回复
热议问题