Using Kanata to remap your keyboard

Thanks to Crafters hanging out on the IRC channel, I learned about Kanata for remapping keys. I use a split mechanical keyboard and miss the conveniences of layers and homerow mods while on my laptop. So far, Kanata has been an excellent option for me.

To continue with my goal of creating more approachable guides to technical topics, I wrote an article on Starting using Kanata to remap any keyboard, with a focus on not only what to do but why we’re doing it and with some background around what these different pieces of configuration mean. My hope is to simplify the first steps for someone without a programming/IT background.

Let me know if you have any feedback or questions!

4 Likes

I tried to use Kanata last night. The config language of both Kanata and KMonad are confusing, not because of the Lisp, but because of having to completely map your keyboard in a table. All I want to do is map AltGr h to ẖ.

To make it more complicated, I’m on a ThinkPad X200 with a UK keyboard.

My kanata.kbd

@Kolev With Kanata you don’t need to map the whole keyboard (not sure about Kmonad), you only put the keys in defsrc that you want to manipulate. See my example here, the order of the keys don’t matter either, they just need to be consistent once you define them in defsrc.
The link above also links to all the keycodes that can be used. You could use AltGr to activate a layer that maps h to ẖ, one way to do it. Or you can just map tap-hold h to produce h on tap and ẖ on hold. I’ll whip up a config to share when I get home.
Attached is the config I’m using to type this.
kanata.kbd (1.5 KB)

2024-04-22T16:47:45.852443261-05:00 [ERROR] Failed to open the output uinput device. Make sure you've added the user executing kanata to the `uinput` group
2024-04-22T16:47:45.853334361-05:00 [ERROR] Permission denied (os error 13)

I tried adding my user to the group.

usermod: group 'uinput' does not exist

EDIT: Followed “Permissions” but still got errors both with and without sudo.

Question: Why not ~/.local/bin/?

@Kolev were you able to do all 5 steps of the permissions section ? Are you still getting a uinput doesn’t exist error? What does the output of groups show? Maybe try a reboot if you don’t see uinput.

You can put the executable wherever you like.

Let me know what errors you’re seeing.

were you able to do all 5 steps of the permissions section ?

Yes.

Are you still getting a uinput doesn’t exist error?

Yes.

What does the output of groups show?

Looks like I’ll have to reboot.

caleb@hermes:~$ groups 
caleb adm cdrom sudo dip plugdev lpadmin sambashare

Errors:

caleb@hermes:~$ kanata
2024-04-22T18:30:47.323460475-05:00 [INFO] kanata v1.5.0 starting
2024-04-22T18:30:47.323496413-05:00 [INFO] You may forcefully exit kanata by pressing lctl+spc+esc at any time. These keys refer to defsrc input, meaning BEFORE kanata remaps keys.
2024-04-22T18:30:47.324345808-05:00 [INFO] process unmapped keys: true
2024-04-22T18:30:47.324701032-05:00 [INFO] NOTE: kanata was compiled to never allow cmd
2024-04-22T18:30:47.33408874-05:00 [INFO] config parsed
2024-04-22T18:30:47.334164698-05:00 [ERROR] Failed to open the output uinput device. Make sure you've added the user executing kanata to the `uinput` group
2024-04-22T18:30:47.334684937-05:00 [ERROR] Permission denied (os error 13)


Press enter to exit
1 Like

Yeah, uinput isn’t showing up as a group. Maybe it’ll show up when you restart.

Rebooting helped. I now show up in groups. It seems to work! However:

  • Holding h did not produce ẖ
  • Right Alt does Backspace
  • Alt+Tab does nothing, just prints ^] to my terminal (interferes with my window manager)

@Kolev can you please share your config file?

Oh you’re using my configuration… yeah, that’s not what you need, I was sharing it for an example.

Sure! Sorry. kanata.kbd

Ah yeah, that’s my config.

May I suggest you go h for single tap and ẖ if you hold down h? Unless if you use vim keys, do you?

Hold down h for ẖ sounds just fine. I do not use Vim keys; I use stock Emacs, pretty much, and I have a preference for the Dvorak layout, which breaks hjkl.

Sounds good, I’ll whip up a config in a bit. I think I know how to do both, I’ll send both files. Give me a bit.

1 Like

Both of what?

Also, I don’t like the idea of having a terminal running it in the foreground all the time. Can I add kanata & to my .xprofile instead?

Hey @Kolev I forgot I had plans tonight, so it took me longer to get back to you. I have uploaded an example configuration which turns right alt key (AltGr) into a layer hold. Right now it only affects the h key but you can map other keys as well if you like. I haven’t tested it (don’t have my laptop now, but where’s the fun if it’s fully done, but you shouldn’t have issues, let me know if you do).

You don’t need to keep a terminal running, you can run it as a system service, the instructions are in the last section of the same guide.

1 Like

systemd loaded service file successfully.

I just get ħħħħħħħħħħ.

@Kolev looks like I go the unicode wrong. You can replace line 25’s 1e96 with the correct unicode sequence. But looks like the layer switch is working as you expect, just printing the wrong character, correct?

ħ is what my normal keyboard layout does when I do AltGr+h.

According to Wikipedia, MACRON BELOW is 0331, so I replaced the number in line 25 with 0331.

 23 ;; shifted layer activated by holding Right Alt (ALtGr) key                                                                
 24 (deflayer altGr
 25         (unicode 0331)
 26         _
 27 )

I restarted Kanata with systemctl --user restart kanata.service, held down AltGr (right Alt) and pressed h. I got ħ again.

@Kolev can you try running Kanata directly in terminal (after stopping it with systemctl) and turn on output for layer change on line 8.

log-layer-changes yes

And see if the layer is switching as you expect it to? I should be able to test it myself tonight but didn’t want to keep you waiting.