You are not logged in.
I'm trying to configure Sway so that I can switch between an arbitrary number of keyboard layouts, with each keyboard layout having its own bindsym. The problem is that Sway claims to have switched my keyboard layout, but nothing actually changes.
Old situation
I have a custom keyboard layout stored in ~/.config/xkb (US keyboard with the Caps Lock key acting as escape, and the Esc key as compose). This is loaded when sway is initialized by the following line in ~/.config/sway/config:
input type:keyboard xkb_file ~/.config/xkb/xkbThis works exactly as it should.
Desired outcome
I want to able to switch between multiple keyboard layouts. For example, type $mod+Alt+r to switch to a Russian keyboard and $mod+Alt+Space to get back to my default. And later on I might add $mod+Alt+g for a Greek keyboard, $mod+Alt+d for Devanagari, and so on and so forth.
To achieve this, I've added the following to ~/.config/sway/config:
input type:keyboard xkb_file ~/.config/xkb/xkb
# Cyrillic keyboard
bindsym $mod+Alt+r input type:keyboard xkb_layout ru
# Latin keyboard (w/ Esc->Compose and CapsLock->Esc)
bindsym $mod+Alt+Space input type:keyboard xkb_file ~/.config/xkb/xkbThe problem
The problem is that input type:keyboard xkb_layout ru does nothing if input type:keyboard xkb_file ~/.config/xkb/xkb has been used previously. If I remove the line input type:keyboard xkb_file ~/.config/xkb/xkb and restart Sway, then $mod+Alt+r works: we switch to Russian. But once I've switched back to my modified Latin, I can't go back to Russian again.
In other words: xkb_layout does not work after xkb_file.
Sway does not give me any error messages. If I hit $mod+Alt+Space (to go to modified Latin) and then type sway input "type:keyboard" xkb_layout ru, it outputs [ { "success": true } ], but the layout is still modified Latin. That is, Sway seems to believe it has changed the keyboard layout, even though it hasn't.
I'd appreciate any help you can give me.
Last edited by myxini (2024-12-09 12:59:35)
Offline
Define all your layouts in your sway config file
input * {
xkb_layout "ca,ca(multix)"
xkb_options "caps:escape,grp:ralt_rshift_toggle"
}If using waybar the sway language module can be use to switch between layouts. Or via keyboard : Alt + Right Shift (xkb_options)
In the terminal or in a script you can use swaymsg but this will replace the layout defined in the config.
swaymsg input type:keyboard xkb_layout "ca(multix)"Last edited by defragmenteur (2024-12-09 00:03:05)
Offline
I don't think that can work. The reason I'm using a custom xkb file is because one of my keyboard desiderata — Esc as the compose key — was not possible to achieve with the available xkb options. Though it was more than a year ago that I wrestled with that, so I may be mistaken. If true, this means I cannot define my main layout in my Sway config file. I have to use a modified xkb file, and my problem remains.
Or to put it differently: if there is a way to remap the "Esc" key to compose (while also mapping the "Caps Lock" key to escape), then I can put all my keyboard layout stuff in my sway config and the problem is solved. But the following, for example, do not work:
input * {
xkb_layout us,ru
xkb_options escape:compose,caps:escape
}input * {
xkb_layout us,ru
xkb_options caps:escape,escape:compose
}In both cases, "Caps Lock" is correctly used as an escape key, but the key labelled "Esc" is not a compose key.
Last edited by myxini (2024-12-09 01:19:38)
Offline
grep compose /usr/share/X11/xkb/rules/base.lstDid you try "caps:swapescape,caps:compose" (I've no idea whether that works, I very much prefer caps as compose)
Otherwise check whether sway picks up your custom layout if you put it at /usr/share/X11/xkb/symbols/us_cec and use that in your config
input * {
xkb_layout us_cec,ru
}Offline
The following worked, until I restarted Sway a second time. Now everything is perfect, except that the Esc key still acts as caps lock. I'll try your second idea later today.
input * {
xkb_layout us,ru
xkb_options caps:swapescape,caps:compose
}
#
# Keyboard layouts:
#
bindsym $mod+BackSpace input type:keyboard xkb_switch_layout next
# Cyrillic keyboard
bindsym $mod+Alt+r input type:keyboard xkb_layout ru
# Latin keyboard
bindsym $mod+Alt+Space input type:keyboard xkb_layout us(This used to be a celebratory post thanking Seth for the solution, but then I had to edit that part out.)
Last edited by myxini (2024-12-09 12:02:28)
Offline
Tried flipping them, "caps:compose,caps:swapescape"?
The problem will be that you're redefining caps twice…
Another thing to check is whether its layout dependent, ie. whether it works w/ one layout but not the other resp. whether it permanently breaks with switching the layout.
Offline
I think I finally solved it.
I installed keyd and put the following in /etc/keyd/default.conf:
[ids]
*
[main]
capslock = esc
esc = capslockand the following in my Sway conf:
input * {
xkb_layout us,ru
xkb_options compose:caps
}
#
# Keyboard layouts:
#
bindsym $mod+BackSpace input type:keyboard xkb_switch_layout next
# Cyrillic keyboard
bindsym $mod+Alt+r input type:keyboard xkb_layout ru
# Latin keyboard
bindsym $mod+Alt+Space input type:keyboard xkb_layout uskeyd switches escape and caps lock at the system level, and then Sway turns "Caps Lock" (which is now the key labelled "Esc") into a compose key.
Thanks for all the help. I'm still a bit puzzled why Sway/xkb can't handle this configuration on their own, without assistance from keyd, but the problem is gone so I shouldn't complain.
Last edited by myxini (2024-12-09 12:56:41)
Offline
Tried flipping them, "caps:compose,caps:swapescape"?
The problem will be that you're redefining caps twice…
I tried this too, but that resulted in there no longer being an escape key...
Anyhow, problem solved.
Offline