How to toggle keyboard layout in Sway from the shell?

6,693

Solution 1

Using sway version 1.5.1, I can provide next to xkb_switch_layout. So you no longer need to get the current index and number of total items.

swaymsg input "1:1:AT_Translated_Set_2_keyboard" xkb_switch_layout next

Unfortunately, I cannot use type:keyboard instead of the identifier. You can find your identifier with swaymsg -t get_inputs. If you want to see all available layouts and the index of the current layout you would need to add --raw.

Solution 2

You can retrieve the current keyboard layout index of your input by running swaymsg -t get_inputs, which will give you a JSON containing an array of inputs and their associated settings.

The keys you are looking for are xkb_active_layout_index and xkb_layout_names.

Here is an example of the output given by the command on my system (cropped to show the relevant part):

  (...)
  {
    "identifier": "16700:8467:Dell_KB216_Wired_Keyboard",
    "name": "Dell KB216 Wired Keyboard",
    "vendor": 16700,
    "product": 8467,
    "type": "keyboard",
    "xkb_layout_names": [
      "English (US)",
      "Romanian (standard)"
    ],
    "xkb_active_layout_index": 0,
    "xkb_active_layout_name": "English (US)",
    "libinput": {
      "send_events": "enabled"
    }
  },
  (...)

To simulate the behavior of sway's toggle, you can use the following code:

inputid="YOUR_KEYBOARD_INPUT_IDENTIFIER"

inputdata=$(swaymsg -t get_inputs | jq ".[] | select(.identifier==\"$inputid\")")
index=$(echo "$inputdata" | jq ".xkb_active_layout_index")
layoutcount=$(echo "$inputdata" | jq ".xkb_layout_names | length")
swaymsg input "$inputid" xkb_switch_layout $((($index + 1) % $layoutcount))

Using sway version 1.4

Share:
6,693

Related videos on Youtube

Robert Kusznier
Author by

Robert Kusznier

Programmer, web developer, vegan. Born in 1989 in Poland. My profiles: GitHub | LeetCode What I like: Creating things, which are useful and solve real problems that our world faces, Learning and trying new things, both in programming and other fields, Clean code, good architecture, good design, JavaScript, Python, Haskell, Vim, People and other creatures, Cooking, David Lynch, Béla Tarr, Tom Waits and other cinema and music, Honesty. What I don't like: Writing and working on crappy code, Things that don't work as they should, Laziness, Wasting things.

Updated on September 18, 2022

Comments

  • Robert Kusznier
    Robert Kusznier over 1 year

    How can I toggle between xkb keyboard layouts in Sway from the command line? My layouts are configured like this:

    input type:keyboard {
        xkb_layout pl,es
        xkb_options grp:win_space_toggle,compose:caps
    }
    

    I'd like to be able to have a command that switches to the next layout (to pl if the current one is es, to es if the current one is pl).

    Note: There is a command swaymsg "input type:keyboard xkb_switch_layout <index>" that allows you to change the layout specifying it's index. I don't know how to use that to toggle to the next one, as I don't know how to get the current index.