Sway: How to get current keyboard layout?

Created on 28 Feb 2017  路  13Comments  路  Source: swaywm/sway

Keyboard layout switching configs described in wiki work well, but I don't know the way to get current layout. Is there any? Thanx.

Most helpful comment

swaymsg -t get_inputs | jq '.[0].xkb_active_layout_name'

That works for me, @ianhattendorf, may you also try it? It though prints full layout name, rather than 2-letters. jq util is form here, you may use any other json utility for that. For me keyboard is the first element in inputs array, so if it differs for you, you should replace [0]

All 13 comments

Just check the environment variable?

Just check the environment variable?

Which one?

XKB_DEFAULT_LAYOUT?

The same one you have to set to specify the layout in the first place.

> echo $XKB_DEFAULT_LAYOUT
us,ru

No, that is not it. I need to know which layout is set, us or ru. I need a way to implement a kbd layout indicator.

Oh. I don't know how to do that.

But you implemented kbd switching somehow, there should be some way to get the value of the current active layout? Some ipc call?

No, that's all done by libinput, not us.

@RichardVoid I don't think that works under Wayland. Testing on i3 works, but on Sway it's always LED mask: 00000000.

For example, I have Exec=env XKB_DEFAULT_LAYOUT=us,br XKB_DEFAULT_OPTIONS=grp:alt_shift_toggle sway in my sway.desktop file. I can toggle fine, however I can't query the current state. With i3, I was using the command xkb-switch -p;xkb-switch -W to get the initial state and then any updates in real-time.

@SirCmpwn Is this something that would have to be handled by Sway/wlroots, or is it possible to query using something like libinput? I'm not seeing anything in it's man pages or online.

swaymsg -t get_inputs | jq '.[0].xkb_active_layout_name'

That works for me, @ianhattendorf, may you also try it? It though prints full layout name, rather than 2-letters. jq util is form here, you may use any other json utility for that. For me keyboard is the first element in inputs array, so if it differs for you, you should replace [0]

@l4l's solution works well, but the keyboard element changes its place randomly (maybe after replugging?). The following query grabs the layout regardless of position:

swaymsg -t get_inputs | jq 'map(select(has("xkb_active_layout_name")))[0].xkb_active_layout_name'

Also, I love Lina as well :)

Yeah, that's still doesn't work if you specify xkb toggle option only for specified identifier. So I ended up with explicit search rather than choose one by luck:

swaymsg -t get_inputs | jq -r '.[] | select(.identifier == "<your_keyboard_identifier>") | .xkb_active_layout_name')
Was this page helpful?
0 / 5 - 0 ratings

Related issues

aidanharris picture aidanharris  路  3Comments

ddevault picture ddevault  路  3Comments

Olie440 picture Olie440  路  4Comments

WhyNotHugo picture WhyNotHugo  路  3Comments

emersion picture emersion  路  4Comments