Python – Detects keyboard layout in *nix in Python

Detects keyboard layout in *nix in Python… here is a solution to the problem.

Detects keyboard layout in *nix in Python

I’m using pykeylogger And wanted to expand it a little by adding information about the currently selected keyboard layout (now you can know from the log which buttons were pressed, let’s say with US qwerty).

For Windows systems, it looks like:

def get_locale(self):
        if os.name == 'nt':
            w = user32. GetForegroundWindow() 
            tid = user32. GetWindowThreadProcessId(w, 0) 
            return hex(user32. GetKeyboardLayout(tid))

Getting the hexadecimal code for a layout like 0x409409 is fine for me because I basically want to distinguish one layout from another.

I would appreciate it if you could give me a solution for a POSIX (e.g. Ubuntu) system.

Solution

setxkbmap – Print

xkb_keymap {
xkb_keycodes  { include "evdev+aliases(qwerty)" };
xkb_types     { include "complete"  };
xkb_compat    { include "complete"  };
xkb_symbols   { include "pc+gb+gr(simple):2+inet(evdev)+terminate(ctrl_alt_bksp)"   };
xkb_geometry  { include "pc(pc105)" };
};

xkb_keycodes { include “evdev+aliases(qwerty)”};

xkb_symbols { contains “pc+gb+ gr
(Simple): 2+Inet(evdev)+Terminate(ctrl_alt_bksp)”};

Related Problems and Solutions