Keyd: Linux Key Remapper
github.com
github.com
I tried remapping a normal letter key's (a) long press to a command+a but it seems to actually trigger the letter "a" before triggering the modifier.
* Mapped both caps lock and return key when they are clicked with another key to act as Ctrl. When they are clicked alone without combination then they work as ususal.
Anyone mind sharing how to avoid such pitfalls? Or going straight to kanata?
I usually use us-intl or us international with alt-gr dead keys for characters like ç áãóê on portuguese, sometimes ñ for spanish. My notebook keyboard is pt-br abnt, but usually I'm on either a keychron k3 pro or a mx keys with US ansi layout.
btw I really liked the idea of using normal keyboards (logitech mx keys, notebook keyboards) like a qmk one.
Also having only one extra layer on keychron is preventing me to do more.
I personally want to be able to use S-c and S-v to copy-paste everywhere, but have a solution that works across all programs: terminal, Emacs, Firefox, etc.
[ids]
*
[main]
capslock=overload(control, esc)
[meta_mac:M]
c=C-insert
v=S-insertI run PopOS
KDE even has good global menu
Some techniques I've liked / made use of (although I support may vary depending on the tool you use):
- Using tap-hold to put Shift/Ctrl/Gui/Alt on home row underneath fdsa (and on the right-hand side, too).
- "Caps Word", where CapsLock functionality is enabled until a non-[A-Za-z0-9_] letter is input.
- Having a 'combo'/'chord' where pressing 'cv' or 'm,' would take me to the previous/next virtual desktop.
This, and KMonad, finally allow me to have e.g. modifier keys on the home row right on my stock laptop keyboard. (Maybe someday Framework laptops will support ergonomic keyboards. Thinkpads never will, I suppose.)
[1] Almost. Many XF86... codes (eg. XF86TouchpadToggle) are not supported, although I suspect hacking the firmware or keyboard-configurator to add them wouldn't be that hard.
What was interesting is that this brings QMYK(?) functionality of different modes to "dumb" keyboards. I think that's really dope!
> For this to work 'setxkbmap -option compose:menu' must be run after keyd has started.
So now I need to run keyd AND setxkbmap, and setup my configuration in /etc/keyd AND ~/.XCompose
One nice feature it has over AutoHotKey[^1] is that it can use Interception (a keyboard filter driver) to handle keys earlier, which lets it work in more situations (e.g across RDP sessions). Interception itself though does have a bug where if you keep adding/removing devices it will stop working until you restart.
1: I've seen an Interception module for AHK, but it's not built in like Kanata's
I thrive off caplock as a toggle for remapped keys. I turn my sdf-234 into a numpad and jkl; for arrows and some other text navigation binds so I never need to veer far from the home row. caps-/ for ~ I can't live without.
Comparison pics below:
https://appleshinja.com/us-jis-hikaku
It also looks like there's an extra key next to the ?-mark key.
I would love to master Ctrl & Alt (plus maybe the "Windows" key) across Linux, mac OS, and Windows, and load the rest with arcane macros.
Just that the Unicomp 101 refuses to break, so I don't have a Windows key either.
But that's precisely how I use these keys: I remap the one at the left of the (always very narrow on japanese keyboard) spacebar to "Hyper".
Note that the USB HID doesn't have the concept of the Hyper key (so I remap it from X11 / xkb).
Now... Some may wonder: why buy a japanese keyboard and not some fancy ergonomic / DIY keyboard? Several reasons. Japanese is kinda a big country and japanese keyboards are here to stay, so my trick shall keep working until long after I'm dead. Japanese laptops: japanese keyboard on the laptop, I now effectively can have a laptop with a "Hyper" key.
And then the main one: the DIY keyboard community can apparently only do variations of Cherry switches or similar switches. My switch of choice is Topre. I didn't find any DIY keyboard with Topre switches.
So I use a japanese keyboard, with Topre switches. And I remap it and there's nothing "japanese" about it anymore.
I am currently remapping keys to have VIM style arrow keys in the middle of the keyboard. That works everywhere but if you add Ctrl and Shift to the mix you it doesn't work everywhere anymore.
It's like pressing the arrow keys.
And it works fine with shift, for as far as any app are concerned they're only seeing "shift + arrow key(s)".
It would work with Super too.
I'll find some part of the .xkb file and paste them here in a few moments.
I first added this new "THREE_HYPER" using three levels (you can add more levels if you fancy that):
type "THREE_HYPER" {
modifiers= Shift+Hyper;
map[Shift]= Level2;
map[Hyper]= Level3;
map[Shift+Hyper]= Level3;
level_name[Level1]= "Base";
level_name[Level2]= "Shift";
level_name[Level3]= "Hyper3";
};
Then the "interpret" does the magic: interpret osfUp {
repeat= True;
action = RedirectKey(keycode=<UP>, clearmodifiers=Hyper);
};
And for each key, assign the arrow key redirection you want (showing only "i" for up here): key <AD08> { type= "THREE_MESH_HYPER", symbols[Group1]= [ i, I, osfUp ] };
I'd say that it's not a job for xmodmap but for .xkb.Also I don't use xmodmap at all: I set everything, including all the modifiers, from my .xkb file.
My xmodmap output for Hyper (which I set, from .xkb, as mod5):
... $ xmodmap | grep mod5
mod5 Hyper_L (0xcf)
You'll want to read on .xkb but this should get you started."Supports X11, Wayland, combinations, programmable macros, joysticks, wheels, triggers, keys, mouse-movements and more."
My use cases are:
- System wide VIM like key mapping
- Access umlauts from my US-keyboard layout
- Key remapping
It's also working great to remap the 10+ buttons of a razer naga mouse
> Linux lacks a good key remapping solution. In order to achieve satisfactory results a medley of tools need to be employed (e.g xcape, xmodmap) with the end result often being tethered to a specified environment (X11). keyd attempts to solve this problem by providing a flexible system wide daemon which remaps keys using kernel level input primitives (evdev, uinput).
So yes it seems to work on both x and wayland
But for your laptop keyboard on your Linux laptop this seems like a great solution
In your case, I'm not sure why you would need to roll over to other keys from escape. My shift key doubles as escape, and I don't use any delay or checks at all since I don't rapidly type escape then other keys. In any case, you could try configuring your capslock key to only act as control if it being held fully overlaps with the next keypress. Not sure about keyd, but kmonad and kanata both support this (I'd highly recommend kanata). That may require changing how you type depending on the modifier, which is why more fine-grained overlap threshold checks can be useful.
Relevant github issues:
I also switched position for Q and escape (q is very rare in Swedish)
Keyboard Maestro would be replaced by one or more completely different tools. There are programs that allow application-specific mapping. I've never seen the point of it, so I can't comment if any of them are any good. What are you looking to do? As someone who uses the keyboard for everything, my experience so far has been that macOS is much weaker at this.
The low-level remapping and higher level app-specific and context-aware functionality is the Holy Grail that's still unachievable in any system, unfortunately, so that's why I specifically asked about the combo
> What are you looking to do
There are way too many interactions for me to list, it's anything from disabling all shortcuts when you play a game to quitting Insert text editor mode when you hold I, to making chord-based command menus with visual cues in apps that don't support anything like that, to inserting special symbols with helpful popups that only popup if you stumble and forget which key is next, to showing a palette with apps to launch with single keys without having to remember all those keys
You simply can't achieve it only via QMK-low-level approach
The maintainer has been very responsive in my experience and would probably add more advanced conditions if someone wanted them.
> There are way too many interactions for me to list
Sounds like mostly hackarounds for non-keyboard-friendly software. I generally prefer to use keyboard-friendly software (of which there is no shortage on linux) and just make application-specific keybindings directly in each application. Anything else is inevitably a hack. Window management, browsing, anything related to text or programming, etc. all have good keyboard-friendly options. I have to use some stuff like excel and teams for work, but I'll just use the mouse more in those cases. I don't spend a significant amount of time dealing with applications like that though, and e.g. for Teams I'd try set up a matrix bridge or just use warpd before trying anything else.
Teams no longer supports linux, but for other apps that have non-customizable key shortcuts, it's definitely possible hack around that and create your own in linux. E.g. When I used to use libreoffice regularly, I set up modal keybindings that sent the corresponding chords to libreoffice.
As long as I can use my editor to type anything non-trivial, I'm good. For example, you have generic software like sketchyvim on mac to essentially use vim in any input field. I don't think there's a linux equivalent that lets you essentially embed an editor like vim inside any input field, but you can use a generic keybinding to open one and then have the text sent back when you're done.
A lot of the examples you list don't seem to need any app-specific or context-aware functionality (e.g. your launcher example). I don't have enough of an idea of what you actually want to know if what's available on linux would support your use cases, but in addition to the numerous basic hotkey daemons there are programs like keyszer that support application-specific keybindings ootb or hawck which is scriptable in lua and supports conditional keybindings (allowing you to script per-application keybindings). You have some context available but only what is externally available, i.e. unless the program provides some way to interact with it like a cli utility or ipc socket, all you're going to be able to do is send input to it.
No way, that's an almost non-existing class of apps. Which "keyboard-friendly software" supports the differentiation between tap and hold? Hold delays? Simultaneous keypresses? Arbitrary alpha key modifiers (hold T for Toolbar and tap 1,2,3 select different types of brushes in your image app)? Support for custom notifications? Knows left from right? Supports chords, not just combos, including single-key shortcuts? Basically, every app would need to add karabiner/katana as a library and slap app-specific contexts on top of that. Which apps do that???
> Anything else is inevitably a hack.
It's also the only way to make something great. There is no "native" OS/app development platform that matches that power, so it's very rare for an app to implement anything advanced on its own
> for other apps that have non-customizable key shortcuts
But it's not a binary, even apps that offer customizable key shortcuts, mostly offer underpowered versions nowhere close to what a keyboard remapper can offer.
> When I used to use libreoffice regularly, I set up modal keybindings that sent the corresponding chords to libreoffice.
But presumably you wouldn't want to send those shortcuts outside of Libreoffice, right? So you key remapper has to be app-aware
> sketchyvim on mac to essentially use vim in any input field.
This is great and I wish all OS had this basic functionality of allowing users to use their advanced text editing tools anywhere (there is also a more limited browser-only, but cross-platform extension GhostText)
> I don't think there's a linux equivalent that lets you essentially embed an editor like vim inside any input field, but you can use a generic keybinding to open one and then have the text sent back when you're done.
Sure, also you can always copy&paste, but that's friction, so not very suitable for smaller edits. Though maybe you can set a keybind that would instantly open a specially configured editor with a very small window size (basically, the size of a text field) without any extra visual stuff like gutter etc.?
> A lot of the examples you list don't seem to need any app-specific or context-aware functionality (e.g. your launcher example)
All of them do, e.g., app launcher requires the contexts of the app state. If the app is in the foreground, it will hide it, if it's in the background, it'll show it, if the app isn't not launched, it'll launch it.
> keyszer
that's only X11
> hawck which is scriptable in lua
thanks for tip, Autohotkey which it aims to replace is great (and lua is better, though also not a great scripting language), so this would be something like Hammerspoon on a Mac. That would still fall short of Keyboard Maestro with its GUI where you can much easier find he available actions and stich them together
> You have some context available but only what is externally available
That's a very common limitation unfortunately
It would be cool, but I just don't see any of this as necessary. What I use supports single-key shortcuts, modality, and modifier shortcuts, and I wouldn't need anything else. Emacs and probably vim support arbitrary key combos and some more advanced functionality with timeouts, but I don't use those features. Even at the global level, I don't use tap-hold on alpha keys or arbitrary combos. It seems like these extra features are primarily useful as workarounds for having a bad keyboard. Normal modifiers are good enough for me. There are already plenty of actions available using just single keys and modifier keys, and modifier keys are comfortable on a good keyboard (even on my personal laptop I have 8 comfortable thumb keys). "hold T for Toolbar and tap 1,2,3" seems like a comparably painful/inefficient chord, and I'm not using software designed primarily with mouse interaction and things like toolbars in mind.
> But it's not a binary, even apps that offer customizable key shortcuts, mostly offer underpowered versions nowhere close to what a keyboard remapper can offer.
I'm definitely biased in that I'm a software engineer and am not working with graphical applications like photoshop. 95% of my time is spent using tools that are programmable and intended to be used primarily with a keyboard or at least have first-class support for it.
> there is also a more limited browser-only, but cross-platform extension GhostText
firenvim can also actually embed vim in browser input fields regardless of OS. Vim is really the only option still if you don't want an external window though.
> Though maybe you can set a keybind that would instantly open a specially configured editor
Right, there are things like emacs-everywhere that can be globally bound and will automatically handle the "syncing." Probably there are similar plugins for other editors but nothing universal I'm aware of.
> All of them do, e.g., app launcher requires the contexts of the app state. If the app is in the foreground, it will hide it, if it's in the background, it'll show it, if the app isn't not launched, it'll launch it.
I have global keybindings for all my most used applications that support toggle/dropdown functionality. I don't really use enough applications to need a launcher that does this, but you can at least implement this using external context only. It would be easy to script something like this on linux, but it would be X11-specific (e.g. tdrop + maybe rofi) or window manager-specific.
> That would still fall short of Keyboard Maestro with its GUI where you can much easier find he available actions and stich them together
Only if you're a fan of GUI configuration. I'd much rather have no GUI at all and just good documentation.
> It's also the only way to make something great. There is no "native" OS/app development platform that matches that power, so it's very rare for an app to implement anything advanced on its own
> > You have some context available but only what is externally available > That's a very common limitation unfortunately
It certainly would be nice if all software was integrated at the level where you could access and work with internal state. It's why I'm using Emacs for nearly everything, but then you're stuck with whatever limitations/flaws the system has, so there are tradeoffs. If you like having options, it's nice that linux has so many Desktop Environment choices and dozens of really good window managers. By comparison, if you like tiling window managers, macOS is not great. Yabai is passable but requires partially disabling SIP or else it can't even manage/switch spaces. On the other hand, a lot of hammerspoon functionality is missing on linux. Linux will probably never have any good generic automation tools. X11 at least has some more advanced, generic tools like xdotool, but this is currently impossible with Wayland, and definitely nothing supports both. Anything more advanced than input like window management is wm/compositor-specific currently on Wayland, so if anything, linux is going backwards wrt generic keyboard/automation tooling.
But I do! Hence my question specifically about tools that match the power of the ones I use that allow the use of a broader class of apps than just emacs
> It seems like these extra features are primarily useful as workarounds for having a bad keyboard. Normal modifiers are good enough for me.
> "hold T for Toolbar and tap 1,2,3" seems like a comparably painful/inefficient chord, and
It's specifically chosen for the T mnemonic (thumb B for toolBar is also fine), which is easier to remember than the more convenient thumb mods, so works better for less frequently used functions
> There are already plenty of actions available using just single keys and modifier keys
Only if you significantly constraint app universe!
> probably vim support arbitrary key combos
Vim doesn't even support left vs right modifiers
> 95% of my time is spent using tools that are programmable and intended to be used primarily with a keyboard or at least have first-class support for it.
That's not first-class, that would be power+usability, and none offer that, you can get to power with a lot of effort and using other tools
> I have global keybindings for all my most used applications
Globals interfere with some uber-global fullscreen apps which you don't want to accidentally interrupt.
> Only if you're a fan of GUI configuration. I'd much rather have no GUI at all and just good documentation.
Or if value your time, for a lot of these functions it'd take much longer reading the docs/writing scripts
> If you like having options, it's nice that linux has so many Desktop Environment choices and dozens of really good window managers.
I like having good options, so having many subpar ones doesn't help. Hence the original question, it takes a lot of time having to review and find limitations of the various options, so thanks for the tips
> linux is going backwards wrt generic keyboard/automation tooling.
:(
Based on what I’ve seen, this isn’t true. macOS supports an amazing amount of keyboard mapping capability.
There’s a serious rabbit hole of macOS keyboard remapping; you’ve been warned: https://brettterpstra.com/topic/keybindings/
Consider window management, for example. If we're talking about using the keyboard for nearly everything, window management is one of the most fundamental parts of interacting with a computer. Linux has dozens of keyboard-based tiling window managers. You have only a few options on macOS, and they are all weak by comparison. Yabai is passable if you partially disable SIP so it can manage spaces, but it still has pretty limited functionality compared to herbstluftwm and bspwm (which is what it's mimicking) and feels hacky.
Similarly, cli tools are very popular on linux, and it is very easy to bind keys to them. Luckily a lot of tools work on both linux and mac, but this is not always the case. Replicating my global hotkeys has been an annoyance on macOS in some cases because of this. In linux, you have control of and multiple options for basically everything (e.g. bootloader, what you use for networking, what you use for notifications, etc.). In the case of notifications, on linux I can just bind a key to "dunstctl close-all" to close all notifications or "dunstctl history-pop" to show old notifications. My notification daemon is designed with scriptability in mind. If I want to have a similar keybinding on macOS I have to go through an arduous process of trying to find some up-to-date applescript snippet that actually works and isn't slow (couldn't find one that met either requirement) or learn to use some other automation tool. Unlike the WM case, I'm sure it's possible, it's just that macOS is just very clearly not designed with the use-case of keyboard for everything in mind. It can still support it very well, but with linux I can have it so that non-keyboard-friendly software just doesn't exist on my system. I don't need to workaround it.