skip to Main Content

I am working with code( C++, ubuntu, gdk, xkb) where I use a german Keyboard which is on the top of the list in settings->Keyboard->input sources .

In my program I can access the keycode, keyval, keymaps used for the language which is on top of that list; (if I change the language I get different results – which is fine and of course should work this way)

Now in my C++ program I want to access the name of the active keyboard like "german" or "french". How can I access the name of the active keyboard using c++ gdk or xkb

2

Answers


  1. To access the name of the active keyboard layout using C++ with GDK or XKB, you can use the following approaches:

    Using GDK:

    1. Get the current GdkSeat: Use the gdk_display_get_default_seat() function to retrieve the default GdkSeat associated with the display.

    2. Get the active GdkKeyboard: Use the gdk_seat_get_keyboard() function to obtain the active GdkKeyboard for the seat.

    3. Get the GdkKeymap: Use the gdk_keyboard_get_keymap() function to retrieve the GdkKeymap associated with the GdkKeyboard.

    4. Get the active layout name: Use the gdk_keymap_get_layout_name() function to obtain the name of the active keyboard layout.

    Here’s an example code snippet that demonstrates this approach:

    #include <gdk/gdk.h>
    
    int main() {
        // Initialize GDK
        gdk_init(nullptr, nullptr);
    
        // Get the default GdkDisplay
        GdkDisplay* display = gdk_display_get_default();
    
        // Get the default GdkSeat
        GdkSeat* seat = gdk_display_get_default_seat(display);
    
        // Get the active GdkKeyboard
        GdkKeyboard* keyboard = gdk_seat_get_keyboard(seat);
    
        // Get the GdkKeymap
        GdkKeymap* keymap = gdk_keyboard_get_keymap(keyboard);
    
        // Get the active layout name
        const gchar* layoutName = gdk_keymap_get_layout_name(keymap);
    
        // Print the active layout name
        g_print("Active keyboard layout: %sn", layoutName);
    
        return 0;
    }
    

    Using XKB:

    1. Open the connection to the X server: Use the XOpenDisplay() function to open a connection to the X server.

    2. Get the active XkbStateRec: Use the XkbGetState() function to retrieve the current XkbStateRec structure.

    3. Get the XkbDescPtr: Use the XkbGetKeyboard() function to obtain the XkbDescPtr structure.

    4. Get the active layout name: Access the names field of the XkbDescRec structure to retrieve the name of the active keyboard layout.

    Here’s an example code snippet that demonstrates this approach:

    #include <X11/Xlib.h>
    #include <X11/XKBlib.h>
    
    int main() {
        // Open the connection to the X server
        Display* display = XOpenDisplay(nullptr);
    
        // Get the current XkbStateRec
        XkbStateRec state;
        XkbGetState(display, XkbUseCoreKbd, &state);
    
        // Get the XkbDescPtr
        XkbDescPtr desc = XkbGetKeyboard(display, XkbAllComponentsMask, XkbUseCoreKbd);
    
        // Get the active layout name
        Atom atom = desc->names->groups[state.group].name;
        char* layoutName = XGetAtomName(display, atom);
    
        // Print the active layout name
        printf("Active keyboard layout: %sn", layoutName);
    
        // Free resources
        XFree(layoutName);
        XkbFreeKeyboard(desc, XkbAllComponentsMask, True);
    
        // Close the connection to the X server
        XCloseDisplay(display);
    
        return 0;
    }
    

    Make sure to link against the appropriate GDK or XKB libraries when compiling your code.

    Note: The GDK approach relies on the GDK library, which is a higher-level abstraction that sits on top of XKB. The XKB approach directly interacts with the XKB extension. Both approaches should give you the desired result, but you may choose the one that best fits your needs and existing codebase.

    Login or Signup to reply.
  2. For Ubuntu, you can read it from /etc/default/keyboard:

    $ cat /etc/default/keyboard
    XKBLAYOUT="us"
    BACKSPACE="guess"
    XKBMODEL="pc105"
    XKBVARIANT=""
    XKBOPTIONS=""
    

    Or use the setxkbmap command:

    $ setxkbmap -query
    rules:      evdev
    model:      pc105
    layout:     us,us
    variant:    ,
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search